2

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?

user305883
  • 457
  • 2
  • 10
  • 4
    Did you want Riffle[#, "."] & /@ list ? – Simon Woods Nov 08 '16 at 21:04
  • 1
    Yes... Could you explain the difference in using Riffle[#, "."] & /@ list then Riffle[".", #] & /@ list ? And why Riffle[{"."}, #] & /@ list replaces with dots instead of "riffling"? – user305883 Nov 08 '16 at 21:29
  • 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} – Simon Woods Nov 09 '16 at 16:53

1 Answers1

3

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"}}

LCarvalho
  • 9,233
  • 4
  • 40
  • 96