Skip to content

Instantly share code, notes, and snippets.

@amar47shah
amar47shah / exercises_test.rb
Last active August 29, 2015 14:23
Testing valid URIs for exercism.io Readme and Test Suite, Two Attempts
# test/acceptance/exercises_test.rb
#
#
# Attempt 1
require_relative '../acceptance_helper'
class ExercisesTest < AcceptanceTestCase
def test_navigation_from_readme_to_readme
visit '/exercises/ruby/phone-number/readme'
click_on 'Readme'
-- Derivation of (f .) . g == f (g x y)
-- Examples
countWhere = (length .) . filter
duplicate = (concat .) . replicate
concatMap = (concat .) . map
-- Could also be written
countWhere p xs = length (filter p xs)
duplicate n xs = concat (replicate n xs)
-- Tail-recursive Fibonacci
fib n = go n (0, 1)
where
-- (x, y) are the last two calculated Fibonacci numbers
-- n is how many more Fibonacci steps to take
-- calling `go` is the final step in evaluating `go (n - 1) (y, x + y)`
go n (x, _) | n < 1 = x
go n (x, y) = go (n - 1) (y, x + y)
@amar47shah
amar47shah / google.rb
Created August 24, 2015 22:09
Testing different HTTP libraries locally because RestClient was suddenly very slow -- resolved by disabling IPv6
require 'curb'
require 'faraday'
require 'open-uri'
require 'rest_client'
def time_request
t = Time.now
yield
puts Time.now - t
end
@amar47shah
amar47shah / clean_database_spec.rb
Last active August 27, 2015 17:30
Test for clean database between specs
# spec/clean_database_spec.rb
require 'rails_helper'
describe 'Checking for clean database' do
specify 'no data to start with' do
[
# your model classes
].each do |model|
expect(model.count).to be_zero
@amar47shah
amar47shah / JoinList.hs
Created October 25, 2015 19:33
How to extract common form...?
module JoinList where
import Sized
import Data.Monoid ((<>))
data JoinList m a = Empty
| Single m a
| Append m (JoinList m a) (JoinList m a)
deriving (Eq, Show)
puts "Welcome to the Game!"
puts "What's your name?"
name = gets.chomp
puts "Hi, #{name}!"
puts "How old are you"
age = gets.chomp
@amar47shah
amar47shah / hanoi.hs
Last active April 24, 2016 00:09
Towers of Hanoi, with command-line interface
-- Towers of Hanoi for three pegs
-- outputs a list of moves to transport a tower of specified size
-- from peg A to peg B, with peg C as reserve space.
--
-- $ runghc hanoi.hs 3
-- [("A","B"),("A","C"),("B","C"),("A","B"),("C","A"),("C","B"),("A","B")]
import System.Environment (getArgs)
main :: IO ()
# View
model = MY_MODEL_NAME
model.methods.select { |method| method.to_s =~ /^_{1}[^_].+_callbacks$/ }
.each_with_object({}) { |method, memo| memo[method] = model.send(method).group_by(&:kind)
.each { |_, callbacks| callbacks.map! { |callback| callback.raw_filter } } }
# Nuke
%i(initialize validate create update save destroy validation find touch commit rollback).each do |event|
reset_callbacks event
end
@amar47shah
amar47shah / hit-hobbit.clj
Last active December 10, 2016 21:40
Hit a hobbit at least once, optionally many times. http://www.braveclojure.com/do-things/#Hobbit_Violence
; > (-main 3)
; Hit you in the right knee!
; Hit you in the left achilles!
; Hit you in the back!
; I think you've had enough.
; => nil
(defn hit
"Expects a nested map with int keys pointing to maps of :name and :size,
and a :max key indicating the smallest strict upper bound of int keys"