Skip to content

Instantly share code, notes, and snippets.

Avatar
🆒

Patrick Oscity padde

🆒
View GitHub Profile
@padde
padde / keybase.md
Created Jul 19, 2015
Keybase proof
View keybase.md

Keybase proof

I hereby claim:

  • I am padde on github.
  • I am padde (https://keybase.io/padde) on keybase.
  • I have a public key whose fingerprint is 7B8E 3327 8E52 E3E1 8392 92AE CCBB 382F 5F9D 1786

To claim this, I am signing this object:

@padde
padde / regex_interpolation.exs
Last active Aug 29, 2015
Elixir Regex Interpolation – do not use in production
View regex_interpolation.exs
defmodule Regex.InterpolationError do
defexception [:message]
end
defmodule Regex.Interpolation do
def wrap(regex = %Regex{opts: modifiers}) do
Regex.source(regex)
|> do_wrap(modifiers)
end
@padde
padde / unicode_pattern_matching.exs
Created Jun 5, 2015
Elixir Unicode Pattern Matching
View unicode_pattern_matching.exs
iex> <<first, rest::binary>> = "☺ how dare you, unicode!"
"☺ how dare you, unicode!"
iex> <<first>>
<<226>>
iex> rest
<<152, 186, 32, 104, 111, 119, 32, 100, 97, 114, 101, 32, 121, 111, 117, 44, 32, 117, 110, 105, 99, 111, 100, 101, 33>>
@padde
padde / devise.rb
Last active Aug 29, 2015
Rails + Devise + RSpec = ❤️
View devise.rb
# spec/support/devise.rb
module DeviseFeatureHelpers
def sign_in(user = nil, scope = :user)
user ||= build_stubbed :user
login_as user, scope: scope
end
end
RSpec.configure do |config|
@padde
padde / mutate.exs
Created Mar 24, 2015
Stream.mutate/3
View mutate.exs
defmodule MyStream do
def mutate(enum, user_acc, user) do
step = fn val, _acc -> {:suspend, val} end
next = &Enumerable.reduce(enum, &1, step)
&do_mutate([], user_acc, user, next, &1, &2)
end
defp do_mutate(values, user_acc, user, next, {:suspend, acc}, fun) do
{:suspended, acc, &do_mutate(values, user_acc, user, next, &1, fun)}
end
@padde
padde / partial_application.rb
Last active Aug 29, 2015
Proc#with – Partial Application
View partial_application.rb
require 'forwardable'
class Proc
def with(*arguments, &block)
proc do |*remaining_arguments|
call(*remaining_arguments, *arguments, &block)
end
end
end
@padde
padde / post.rb
Created Jan 8, 2015
Post model with pg_search
View post.rb
class Post < ActiveRecord::Base
include PgSearch
pg_search_scope :search,
against: :body,
using: {tsearch: {dictionary: 'german'}}
end
@padde
padde / tsvector_german.sql
Created Jan 8, 2015
to_tsvector (german)
View tsvector_german.sql
SELECT to_tsvector('german', 'Ich würde gerne schwimmen gehen');
------------------------------
'geh':5 'gern':3 'schwimm':4
@padde
padde / tsvector_german_broken.sql
Created Jan 8, 2015
to_tsvector (german with english analysis)
View tsvector_german_broken.sql
SELECT to_tsvector('Ich würde gerne schwimmen gehen');
----------------------------------------------------
'gehen':5 'gern':3 'ich':1 'schwimmen':4 'würde':2
View tsvector.sql
SELECT to_tsvector('I like to go swimming');
--------------------------
'go':4 'like':2 'swim':5