Last active
February 9, 2018 22:10
-
-
Save ddrscott/bac12bb1788d8a85a1bbe191151fedd5 to your computer and use it in GitHub Desktop.
Grade school using funky lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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