2
$\begingroup$

I want to learn how to apply core functions with multiple parameters with no need to name new functions taking multiple variables.

I looked at Mapping multiple parameters of a function to specific values , How to Map a subset of list elements to a function? and would like to iterate to a list of lists.

This is my example.

I have a list of lists:

{{"Absolute risk reduction", "Medicine"}, {"Absorption cross section","Medicine"}, {"Abutment", "Dentistry", "Medicine"}}

I want to use Riffle[a, b] and apply

Riffle[{"Abutment", "Dentistry", "Medicine"}, "."]

for each of them, here b = "."

I can map a function with one parameter, but how to with multiple ones in one line?

Desired output:

{{"Abutment", ".", "Dentistry", ".", "Medicine"} .. }

I tried:

(* Map[Reverse, %180] is the lists of list evaluation *)

Riffle[{"."}, #] & /@ Map[Reverse, %180]
(* {{"."}, {"."}, {"."}}*)

Map[Riffle[{"."}, #] &, Map[Reverse, %180]]
(* {{"."}, {"."}, {"."}}*)

Riffle @@ {Map[Reverse, %180][[#]], {"."}} &
(* Riffle @@ {(Reverse /@ %180)[[#1]], {"."}} & *)

Why the first two returns a list of lists of dots and not the desired output?

Can you explain what is doing the third syntax?

$\endgroup$
3
  • 4
    $\begingroup$ Did you want Riffle[#, "."] & /@ list ? $\endgroup$ Commented Nov 8, 2016 at 21:04
  • 1
    $\begingroup$ Yes... Could you explain the difference in using Riffle[#, "."] & /@ list then Riffle[".", #] & /@ list ? And why Riffle[{"."}, #] & /@ list replaces with dots instead of "riffling"? $\endgroup$
    – user305883
    Commented Nov 8, 2016 at 21:29
  • $\begingroup$ Riffle[list, item] returns list with item inserted into the "gaps" between elements. If list is only one element long, there are no gaps, so list is returned unchanged. This is noted in the documentation for Riffle: Riffle[{e}, x] gives {e} $\endgroup$ Commented Nov 9, 2016 at 16:53

1 Answer 1

3
$\begingroup$

As already commented by Simon Woods.

Here is the result using the examples given:

words = {{"Absolute risk reduction", 
   "Medicine"}, {"Absorption cross section", "Medicine"}, {"Abutment",
    "Dentistry", "Medicine"}}

{{"Absolute risk reduction", "Medicine"}, {"Absorption cross section", "Medicine"}, {"Abutment", "Dentistry", "Medicine"}}

Riffle[#, "."] & /@ words

{{"Absolute risk reduction", ".", "Medicine"}, {"Absorption cross section", ".", "Medicine"}, {"Abutment", ".", "Dentistry", ".", "Medicine"}}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.