Skip to main content
Post Closed as "Duplicate" by Mr.Wizard
edited tags
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

How can I do it without introducing test[p_]? In other words I need Select's second argument to depend on "another" slot #, not the one that will be substituted by list elements during iteration. The closest question I was able to find is Second level depth pure function?Second level depth pure function?, but I think my question is slightly different, because I don't have to put slot in the first argument of Select

How can I do it without introducing test[p_]? In other words I need Select's second argument to depend on "another" slot #, not the one that will be substituted by list elements during iteration. The closest question I was able to find is Second level depth pure function?, but I think my question is slightly different, because I don't have to put slot in the first argument of Select

How can I do it without introducing test[p_]? In other words I need Select's second argument to depend on "another" slot #, not the one that will be substituted by list elements during iteration. The closest question I was able to find is Second level depth pure function?, but I think my question is slightly different, because I don't have to put slot in the first argument of Select

added 1634 characters in body
Source Link
BlacKow
  • 6.5k
  • 20
  • 33

Update: Answers extracted from comments.

Thanks a lot to everybody who answered in comments.

leo[l_] := Reap[Sow[#, #] & /@ l, 1 | 8, #2 &][[2]]; jm[l_] := Function[p, Select[l, # == p &]] /@ {8, 1}; kgl[l_] := Select[l, Function[x, x == #]] & /@ {8, 1}; mar[l_] := With[{p = #}, Select[l, # == p &]] & /@ {8, 1}; 

Out of curiosity I decided to run simple benchmark to check performance

Benchmark[f_, n_] := Module[{l, results, samples}, RandomSeed[314]; samples = Table[RandomInteger[{1, 100}, n], {10}]; results = Table[First@AbsoluteTiming[f[l]], {l, samples}]; Mean[results]]; testRange = 10^# &@{3, 4, 5, 6}; TableForm[ Table[Benchmark[fun, n]/n, {fun, {leo, jm, kgl, mar}}, {n, testRange}], TableHeadings -> {{"leo", "jm", "kgl", "mar"}, testRange}] 

enter image description here

The results are normalized over list length. Interestingly kgl[] is about two times slower than jm[].

IMHO the Select[l, Function[x, x == #]] & /@ {8, 1} (by @kglr) is the most visually appealing.

It was also asked why do I care if all I need is just making two simple Selects?

I think it's more clear and concise to have one line that does something twice rather than having two almost identical lines. My original example is oversimplified probably.

Imagine I want to select all prime numbers and also all numbers that are prime squared. If I use syntax by @kglr I can do

{primes, primeSq} = Select[l, Function[x, #[x]]] & /@ {PrimeQ, PrimeQ@Sqrt[#] &} 

Which is very self explanatory.

Update: Answers extracted from comments.

Thanks a lot to everybody who answered in comments.

leo[l_] := Reap[Sow[#, #] & /@ l, 1 | 8, #2 &][[2]]; jm[l_] := Function[p, Select[l, # == p &]] /@ {8, 1}; kgl[l_] := Select[l, Function[x, x == #]] & /@ {8, 1}; mar[l_] := With[{p = #}, Select[l, # == p &]] & /@ {8, 1}; 

Out of curiosity I decided to run simple benchmark to check performance

Benchmark[f_, n_] := Module[{l, results, samples}, RandomSeed[314]; samples = Table[RandomInteger[{1, 100}, n], {10}]; results = Table[First@AbsoluteTiming[f[l]], {l, samples}]; Mean[results]]; testRange = 10^# &@{3, 4, 5, 6}; TableForm[ Table[Benchmark[fun, n]/n, {fun, {leo, jm, kgl, mar}}, {n, testRange}], TableHeadings -> {{"leo", "jm", "kgl", "mar"}, testRange}] 

enter image description here

The results are normalized over list length. Interestingly kgl[] is about two times slower than jm[].

IMHO the Select[l, Function[x, x == #]] & /@ {8, 1} (by @kglr) is the most visually appealing.

It was also asked why do I care if all I need is just making two simple Selects?

I think it's more clear and concise to have one line that does something twice rather than having two almost identical lines. My original example is oversimplified probably.

Imagine I want to select all prime numbers and also all numbers that are prime squared. If I use syntax by @kglr I can do

{primes, primeSq} = Select[l, Function[x, #[x]]] & /@ {PrimeQ, PrimeQ@Sqrt[#] &} 

Which is very self explanatory.

Tweeted twitter.com/StackMma/status/710927770197561344
added 67 characters in body
Source Link
BlacKow
  • 6.5k
  • 20
  • 33
Loading
added 176 characters in body
Source Link
BlacKow
  • 6.5k
  • 20
  • 33
Loading
Source Link
BlacKow
  • 6.5k
  • 20
  • 33
Loading