Skip to content

Instantly share code, notes, and snippets.

Avatar
💭

Guillermo Iguaran guilleiguaran

💭
View GitHub Profile
View maybe.rb
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
View array.rb
class Array
def self.unit(value)
[value]
end
def bind(&block)
flat_map(&block)
end
end
View identity.rb
class Identity
attr_reader :value
def initialize(value)
@value = value
end
private_class_method :new
def self.unit(value)
new(value)
View monoids.hs
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)
View index.html.erb
<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>
View example.ex
defmodule Bot do
@commands = %{"a" => :do_a, "b" => :do_b}
def match(command) do
execute(@commands[command])
end
def execute(:do_a) do
end
View ex.rb
$global_value = 0
def rq(x)
$global_value = $global_value + 1
x + $global_value
end
def rt(x)
x + 1
end
View stateful.rb
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
View gist:fbc6a728c768fa0f877f
~/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
View after.rb
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