Skip to content

Instantly share code, notes, and snippets.

View guilleiguaran's full-sized avatar
💭

Guillermo Iguaran guilleiguaran

💭
View GitHub Profile
@guilleiguaran
guilleiguaran / maybe.rb
Last active September 7, 2015 04:18
Maybe Monad
require 'singleton'
class Maybe
def self.unit(value)
if value.nil? || (value.respond_to?(:empty?) && value.empty?)
Nothing.instance
else
Just.new(value)
end
end
@guilleiguaran
guilleiguaran / array.rb
Last active August 29, 2015 14:24
Array Monad
class Array
def self.unit(value)
[value]
end
def bind(&block)
flat_map(&block)
end
end
@guilleiguaran
guilleiguaran / identity.rb
Last active August 29, 2015 14:24
Identity Monad
class Identity
attr_reader :value
def initialize(value)
@value = value
end
private_class_method :new
def self.unit(value)
new(value)
import Data.Monoid
-- Max: Monoid of Num under max.
data Max a = Max a | MinInfinity deriving Show
instance (Num a, Ord a) => Monoid (Max a) where
mempty = MinInfinity
MinInfinity `mappend` m = m
m `mappend` MinInfinity = m
Max x `mappend` Max y = Max (Prelude.max x y)
<p id="notice"><%= notice %></p>
<h1>Listing Tweets</h1>
<section id="tweets">
<% @tweets.each do |tweet| %>
<article class="tweet">
<p>@<%= tweet.user.username %></p>
<p><%= tweet.body %></p>
<p>
defmodule Bot do
@commands = %{"a" => :do_a, "b" => :do_b}
def match(command) do
execute(@commands[command])
end
def execute(:do_a) do
end
$global_value = 0
def rq(x)
$global_value = $global_value + 1
x + $global_value
end
def rt(x)
x + 1
end
def average_age
return 0 if @group.empty?
sum = @group.map{|x| x.age}
.reduce(0.0){|sum, x| sum + x}
sum / @group.size
end
~/src/app % mix deps.get && mix test
* Updating rabbit_common (git://github.com/jbrisbin/rabbit_common.git)
Running dependency resolution
Dependency resolution completed successfully
rabbit_common: v3.4.0
Unchecked dependencies for environment test:
* rabbit_common (git://github.com/jbrisbin/rabbit_common.git)
lock outdated: the lock is outdated compared to the options in your mixfile
** (Mix) Can't continue due to errors on dependencies
def cities_in_region(region_id)
Regions.lookup(region_id)
.or(Left("Couldn't find region #{region_id}"))
>-> region { region.bounding_box }
.or(Left("Region #{region_id} has no bounding box"))
>-> bounding_box { GeoNames.search_cities(bounding_box) }
end