Skip to content

Instantly share code, notes, and snippets.

@dskecse
dskecse / MathFunc.hs
Last active August 29, 2015 14:05
Implementations of math function in Haskell
-- | ln (abs(sin(x))), if x > 5
-- y = | x^2 + a^2, if x <= 5 and a <= 3
-- | x / a + 7.8*a, if x <= 5 and a > 3
y x a = if x > 5
then log (abs (sin (x)))
else
if x <= 5 && a <= 3
then x^2 + a^2
else x / a + 7.8 * a
@dskecse
dskecse / ConvertStringToAction.hs
Created August 16, 2014 23:08
Implementations of a function that converts a String to an Action
convertStringToAction :: String -> Action
convertStringToAction str = case str of
"Look" -> Look
"New" -> New
otherwise -> Quit
-- guards (охранные выражения)
convertStringToAction' :: String -> Action
convertStringToAction' str | str == "Look" = Look
| str == "New" = New
require 'benchmark/ips'
ARRAY = (1..10).to_a
Benchmark.ips do |x|
x.report('each') { sum = 0; ARRAY.each { |n| sum += n }; sum }
x.report('reduce') { ARRAY.reduce(0, :+) }
end
Calculating -------------------------------------
@dskecse
dskecse / _html5boilerplate.css.sass
Created October 12, 2012 13:27 — forked from richardvenneman/_html5boilerplate.css.sass
HTML5 Boilerplate HAML & SASS
/*
* HTML5 ✰ Boilerplate
*
* What follows is the result of much research on cross-browser styling.
* Credit left inline and big thanks to Nicolas Gallagher, Jonathan Neal,
* Kroc Camen, and the H5BP dev community and team.
*
* Detailed information about this CSS: h5bp.com/css
*
* ==|== normalize ==========================================================
defmodule MyEnum do
def all?([head | tail], func \\ fn x -> x end) do
func.(head) && all?(tail, func)
end
def all?([], _), do: true
def each([], _), do: []
def each([head | tail], func) do
[func.(head), each(tail, func)]
end
defmodule Recursive do
def map([], _func), do: []
def map([head | tail], func), do: [func.(head) | map(tail, func)]
end
defmodule Recursive do
def len([]), do: 0
def len([_head | tail]), do: 1 + len(tail)
end
IO.puts Recursive.len([1, 2, 3])
# => 3
@dskecse
dskecse / libs.exs
Last active October 12, 2015 13:00
:io.format "The number is ~.2f~n", [5.578]
# => "The number is 5.58"
System.get_env "RUBYOPT"
# => "-rauto_gem"
Path.extname "dave/test.exs"
# => ".exs"
System.cwd
@dskecse
dskecse / ransack.rb
Created November 23, 2012 11:33 — forked from ledermann/ransack.rb
Ransack with scopes
# Patch for ransack (https://github.com/ernie/ransack) to use scopes
# Helps migrating from Searchlogic or MetaSearch
# Place this file into config/initializer/ransack.rb of your Rails 3.2 project
#
# Usage:
# class Debt < ActiveRecord::Base
# scope :overdue, lambda { where(["status = 'open' AND due_date < ?", Date.today]) }
# end
#
# Ransack out of the box ignores scopes. Example:
@dskecse
dskecse / decorators.py
Last active November 4, 2015 13:25
Decorators in Python
def honorific(klass):
class HonorificClass(klass):
def full_name(self):
return 'Dr. ' + super(HonorificClass, self).full_name()
return HonorificClass
@honorific
class Person(object):
def __init__(self, first, last):
self.first = first