Skip to main content
5 of 7
Amended Partition arguments
Chris Degnen
  • 31.4k
  • 2
  • 57
  • 112

Split[ls, fn] calls fn with items of Partition[ls,2,1], so if we create a temporary variable (here as i) that in every comparison, the first value gets added to the i and if i + second element is under 20, it returns True meaning keeping them together, otherwise, i=0 and return False, meaning adding a breakpoint.

Block[{i = 0}, Split[alist, (i += #1; If[i + #2 < 20, True, i = 0; False]) &] ] (* Out: {{2, 5, 1, 8, 1, 1}, {9, 7, 1}, {5, 2, 9}, {6, 2, 2, 2, 4, 3}, {2, 7}} *) 

Using @rhermans benchmark code on Ryzen 1700 (AbsoluteTiming):

enter image description here

Ben Izd
  • 9.7k
  • 1
  • 16
  • 47