8

Given

t1 = {2, 4, 8, 16};
t2 = {1, 5, 9};
First[Select[t1, # > 1 &]]     
First[Select[t1, # > 5 &]]
First[Select[t1, # > 9 &]]

can somehow be summarized by

 Table[First[Select[t1, # > x &]], {x, t2}]

to get the correct result

{2, 8, 16}

Is there a way to use two pure functions connected instead of working around the problem by using Table? Something like (which does not work!):

First[Select[t1, # > # &] & /@ t2]
user57467
  • 2,708
  • 6
  • 12

3 Answers3

12

Here are 2 suggestions:

First @ Select[t1, GreaterThan[#]] & /@ t2
Function[x, First @ Select[t1, # > x &]] /@ t2

Edit

Or, if you want to go really abstract:

Map[
 First@Select[t1,
    OperatorApplied[Function[#1 > #2]][#1]
    ] &,
 t2
 ]
Sjoerd Smit
  • 23,370
  • 46
  • 75
7

Just another way to do this using GroupBy and Lookup:

Lookup[GroupBy[t1, GreaterThan[#], First] & /@ t2, True]

({2, 8, 16})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
3
a = {2, 4, 8, 16};

b = {1, 5, 9};

Using FirstCase

FirstCase[a, x_ /; x > #] & /@ b

{2, 8, 16}

eldo
  • 67,911
  • 5
  • 60
  • 168