Skip to main content
added 1 character in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &&;
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &&;

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

Timings:Timings:

Using aardvark2012's setup with SeedRandom[1]

functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where

pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

is suggested by tomd in a comment.

SeedRandom[1]
T1 = RandomInteger[{-5, -1}, {100000, 3}]~Join~{{2, 4, 4}};
T2 = RandomInteger[{-5, 5}, {100000, 3}];
functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where tomd's suggestion pick is

 pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

enter image description here

Equal @@ (#[T1]& /@ functions)

True

Equal @@ (#[T2]& /@ functions)

True

ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

Timings: Using aardvark2012's setup with SeedRandom[1]

SeedRandom[1]
T1 = RandomInteger[{-5, -1}, {100000, 3}]~Join~{{2, 4, 4}};
T2 = RandomInteger[{-5, 5}, {100000, 3}];
functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where tomd's suggestion pick is

 pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

enter image description here

Equal @@ (#[T1]& /@ functions)

True

Equal @@ (#[T2]& /@ functions)

True

ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &;
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &;

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T == lenwhile2 @ T

True

Timings:

Using aardvark2012's setup with

functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where

pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

is suggested by tomd in a comment.

SeedRandom[1]
T1 = RandomInteger[{-5, -1}, {100000, 3}]~Join~{{2, 4, 4}};
T2 = RandomInteger[{-5, 5}, {100000, 3}];

enter image description here

Equal @@ (#[T1]& /@ functions)

True

Equal @@ (#[T2]& /@ functions)

True

added 471 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

Timings: Using aardvark2012's setup with SeedRandom[1]

SeedRandom[1]
T1 = RandomInteger[{-5, -1}, {100000, 3}]~Join~{{2, 4, 4}};
T2 = RandomInteger[{-5, 5}, {100000, 3}];
functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where tomd's suggestion pick is

 pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

enter image description here

Equal @@ (#[T1]& /@ functions)

True

Equal @@ (#[T2]& /@ functions)

True

ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

Timings: Using aardvark2012's setup with SeedRandom[1]

SeedRandom[1]
T1 = RandomInteger[{-5, -1}, {100000, 3}]~Join~{{2, 4, 4}};
T2 = RandomInteger[{-5, 5}, {100000, 3}];
functions = {firstcase, lenwhile1, lenwhile2, pick, select, selfirst, throwcatch};

where tomd's suggestion pick is

 pick =  Pick[#, Sign[#[[All, 1]]], 1][[1]] &;

enter image description here

Equal @@ (#[T1]& /@ functions)

True

Equal @@ (#[T2]& /@ functions)

True

added 175 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &@T&
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

#[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &@T

{2, 4, 4}

ClearAll[lenwhile1, lenwhile2]
lenwhile1 = #[[1 + LengthWhile[#[[All, 1]], # <= 0 &]]] &
lenwhile2 = #[[1 + LengthWhile[#, #[[1]] <= 0 &]]] &

lenwhile1 @ T

{2, 4, 4}

lenwhile1 @ T ==     lenwhile2 @ T

True

deleted 3 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading