My attempt to work through ninety-nine Haskell problems
This file contains hidden or 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
| # Using insertion | |
| class Array | |
| # Insert into the array. | |
| def insert_every!(skip, str) | |
| (skip...size).step(skip).with_index {|n, i| insert(n + i, str) } | |
| self | |
| end | |
| # Create a new array and insert into it. | |
| def insert_every(skip, str) |
This file contains hidden or 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
| str = 'foo,bar,bar-foo,baz^key=value^key1=value1,bazfoo' | |
| new_str = Hash[str.split(',').map{|s| k,*v = s.split('^'); [k.to_sym,v.empty? ? nil : Hash[v.map {|s|s.split('=')}.inject({}){|c,(k,v)| c[k.to_sym] = v; c}]]}] if str |
This file contains hidden or 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
| module ObjectSpace | |
| class << self | |
| def find_references(target) | |
| each_object.select {|obj| references?(obj, target) } | |
| end | |
| def references?(object, target) | |
| contains = case object | |
| when Hash | |
| object.any? {|k, v| references?(k, target) || references?(v, target) } |
This file contains hidden or 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
| module Token | |
| def self.create(user) | |
| user.guid | |
| end | |
| end | |
| class User | |
| attr_accessor :token, :guid | |
| def initialize(guid) | |
| @guid = guid |
This file contains hidden or 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
| puts <<-END_OF_TEXT | |
| hello | |
| there | |
| END_OF_TEXT | |
| Omit the '-' if you want the final newline. |
This file contains hidden or 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
| classes = if update.classes? | |
| if update[:classes].include? "DELETE" | |
| nil | |
| elsif person['data']['classes'].nil? or person['data']['classes'].empty? | |
| update[:classes] | |
| else | |
| (person['data']['classes'] | update[:classes]).sort | |
| end | |
| else | |
| person['data']['classes'] |
This file contains hidden or 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
| # i want to keep ip and gw, but i want to avoid repeating from "adapters.each" to netconnectionsid.each in both of them. lambdas? | |
| general do |netconnectionid| | |
| Nic::IPConfig.enablestatic(netconnectionid, | |
| :ip => myconfig[:ipaddress], | |
| :mask => myconfig[:subnetmask]) | |
| end | |
| general do |netconnectionid| | |
| Nic::IPConfig.setgateways(netconnectionid, |
This file contains hidden or 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
| class Fixnum | |
| def ordinal | |
| %w[first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth][self - 1] | |
| end | |
| # Use #to_word rather than #to_s, so it doesn't break any other printing of numbers. | |
| def to_word | |
| %w[one two three four five six seven eight nine ten eleven twelve][self - 1] | |
| end | |
| end |
This file contains hidden or 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
| #these are meant to be run in a REPL, and the java one in beanshell of something of the sort: | |
| #ruby | |
| [1,2,3,4].select &:even? | |
| #python | |
| [x for x in [1,2,3,4] if not x%2] | |
| #or, more norvingly | |
| filter(lambda x: not x%2, [1,2,3,4]) | |
| #clojure |