This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extend(destination, source) { | |
for (var meth in source) { | |
if (source.hasOwnProperty(meth)) { | |
destination[meth] = source[meth]; | |
} | |
} | |
return destination; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'English' | |
module NoDoubleRaisable | |
def error_handled! | |
$ERROR_INFO = nil | |
end | |
def raise(*args) | |
if $ERROR_INFO && args.first != $ERROR_INFO | |
warn "Double raise at #{caller.first}, aborting." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Recursive do | |
def map([], _func), do: [] | |
def map([head | tail], func), do: [func.(head) | map(tail, func)] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Recursive do | |
def len([]), do: 0 | |
def len([_head | tail]), do: 1 + len(tail) | |
end | |
IO.puts Recursive.len([1, 2, 3]) | |
# => 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 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 |