My attempt to work through ninety-nine Haskell problems
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
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 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 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 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 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 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 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 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 |
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
-- Write a program that prints the numbers from 1 to 100. | |
-- But for multiples of three print “Fizz” instead of the number and for the multiples | |
-- of five print “Buzz”. For numbers which are multiples of both three and five | |
-- print “FizzBuzz”." | |
-- Generate the Output for a single index. | |
fizzBuzzN :: Int -> String | |
fizzBuzzN x | |
| and $ map multipleOf [3, 5] = "FizzBuzz" | |
| multipleOf 3 = "Fizz" |