Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active February 9, 2018 22:10
Show Gist options
  • Save ddrscott/bac12bb1788d8a85a1bbe191151fedd5 to your computer and use it in GitHub Desktop.
Save ddrscott/bac12bb1788d8a85a1bbe191151fedd5 to your computer and use it in GitHub Desktop.
Grade school using funky lambda
alias λ lambda
sadd = λ {|ns, a| [*ns, a].sort }
hsort = λ {|h| Hash[h.sort] }
hadd = λ {|h, n, g| h.merge(g => sadd.(h[g], n)) }
school = λ {|gs| School.new(gs) }
School = Struct.new(:gs) do
define_method :add, λ {|n, g| (school . (hsort . (hadd . (to_hash, n, g)))) }
define_method :to_hash, λ {| | gs || {} }
define_method :grade, λ {|g| to_hash[g] || [] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment