Skip to content

Instantly share code, notes, and snippets.

@madlep
madlep / fib.ex
Created December 21, 2022 18:05
fib benchmarks
defmodule Fib do
defmodule MLibby do
def number(n) do
[head | _] = _sequence(n)
head
end
def sequence(n), do: Enum.reverse(_sequence(n))
defp _sequence(0), do: [0]
defp _sequence(1), do: [1, 0]
# example input
["+3", "+3", "+4", "-2", "-4"]
|> Enum.map(&String.to_integer/1)
|> Stream.cycle()
|> Enum.reduce_while({0, MapSet.new([0])}, fn n, {sum, seen} ->
new_sum = sum + n
if MapSet.member?(seen, new_sum) do
{:halt, new_sum}
else
@madlep
madlep / previous_fibonacci.livemd
Created June 22, 2022 01:54
Given a Fibonacci number, give the previous Fibonacci number. If the number given is not a Fibonacci number, return -1.

Previous Fibonacci Number

Problem

Given a Fibonacci number, give the previous Fibonacci number. If the number given is not a Fibonacci number, return -1.

From rendezvous with cassidoo, June 20 2022

Code

@madlep
madlep / equal_with_deletions.livemd
Last active April 5, 2022 01:47
equal_with_deletions : Given two strings n and m, return true if they are equal when both are entered into text editors. But: a # means a backspace character (deleting backwards), and a % means a delete character (deleting forwards). From https://buttondown.email/cassidoo/archive/5-time-moves-slowly-but-passes-quickly-alice/

equal with deletions

Question

From rendezvous with cassidoo, April 4 2022 "Interview Question Of The Week"

Given two strings n and m, return true if they are equal when both are entered into text editors. But: a # means a backspace character (deleting backwards), and a % means a delete character (deleting forwards).

@madlep
madlep / applicative.rb
Last active January 11, 2021 01:12
Ruby validation applicative example
class Either
def self.pure(value)
Right.new(value)
end
class Left
def initialize(left)
@left = left
end
POINTS = {
"A" => 1, "B" => 3, "C" => 3, "D" => 2,
"E" => 1, "F" => 4, "G" => 2, "H" => 4,
"I" => 1, "J" => 8, "K" => 5, "L" => 1,
"M" => 3, "N" => 1, "O" => 1, "P" => 3,
"Q" => 10, "R" => 1, "S" => 1, "T" => 1,
"U" => 1, "V" => 4, "W" => 4, "X" => 8,
"Y" => 4, "Z" => 10
}
@madlep
madlep / insert_conflict_test.rb
Created May 31, 2017 01:14
#insert_conflict with empty update hash
require "sequel"
DB = Sequel.postgres("sequel_test")
DB.create_table? :items do
primary_key :id
String :name
Float :price
end

Keybase proof

I hereby claim:

  • I am madlep on github.
  • I am madlep (https://keybase.io/madlep) on keybase.
  • I have a public key whose fingerprint is 6A92 159D 5E40 7094 FABE B73A 0B4D 44BE 816C C67C

To claim this, I am signing this object:

@madlep
madlep / foobar.ex
Created November 17, 2015 10:34
which is preferable style: destructuring struct with pattern matching in function definition, or accessing struct members in function body?
defmodule Foobar do
defstruct :my_field, :another_field
def do_a_thing_destructure(%Foobar{my_field: mf, another_field: af}) do
something_else(mf, af)
end
def do_a_thing_struct_access(foobar = %Foobar{}) do
something_else(foobar.my_field, foobar.another_field)
end