Skip to content

Instantly share code, notes, and snippets.

@Ke-
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save Ke-/10323467 to your computer and use it in GitHub Desktop.

Select an option

Save Ke-/10323467 to your computer and use it in GitHub Desktop.
// Inject construct similar to Ruby's inject.
define trait_foreach->inject(p::any) => {
local(gb) = givenblock
#gb->detach
.foreach => {
#p = #gb(#p,#1) || #p
}
return #p
}
// Lasso
array(1,2,3,4)->inject(0) => { return #1 + #2 } // 10
// Ruby
// [1, 2, 3, 4].inject(0) { |result, element| result + element } // 10
array(1,2,3,4)->inject(array) => { #1->insert('element: '+ #2) }
// array(element: 1, element: 2, element: 3, element: 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment