Skip to content

Instantly share code, notes, and snippets.

@franzejr
franzejr / counter-react.js
Created March 6, 2016 03:40
Counter ReactJS
import React, { Component, PropTypes } from 'react'
class Counter extends Component {
constructor(props) {
super(props)
this.incrementAsync = this.incrementAsync.bind(this)
this.incrementIfOdd = this.incrementIfOdd.bind(this)
}
incrementIfOdd() {
@franzejr
franzejr / reducer-counter.js
Created March 6, 2016 03:35
Reducer Counter
export default function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
@franzejr
franzejr / counter.elm
Created March 6, 2016 03:21
Counter Elm
module Counter where
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
-- MODEL
type alias Model = Int
@franzejr
franzejr / example-architecture.elm
Created March 6, 2016 03:18
Elm - three parts
-- MODEL
type alias Model = { ... }
-- UPDATE
type Action = Reset | ...
update : Action -> Model -> Model
@franzejr
franzejr / my_controller.rb
Last active January 10, 2016 13:55
Using JS.ERB in Ruby (sick)
def my_controller_method
...
respond_to do |format|
format.js{
render :template => 'create.js.erb'
}
end
end
@franzejr
franzejr / higher_order_functions.rb
Created December 29, 2015 02:33
Higher Order Functions in Ruby
# takes one or more functions as arguments
sum_of_powers = ->(a, b, power_function){
power_function.(a) + power_function.(b)
}
power_of_2 = ->(x){ x * x }
puts sum_of_powers.(3 , 2, power_of_2)
@franzejr
franzejr / tail_call.rb
Created December 27, 2015 01:33
Tail Call Optimization in Ruby
RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true,
trace_instruction: false }
eval<<end
def factorial(n, result=1)
if n==1
result
else
factorial(n-1, n*result)
end
end
@franzejr
franzejr / sort_sort_by_dates.rb
Created December 16, 2015 12:01
Sort and Sort_by for dates
require 'active_support/all'
require 'benchmark'
dates = []
5.times {
dates << Time.at(rand * Time.now.to_i).utc
}
puts 'DESC SORT WITH COMPARATOR'
puts dates.sort {|x,y| y <=> x }
@franzejr
franzejr / gist:4d31cce679beb8a4d2ff
Created December 15, 2015 13:22
Git Pre-commit
gem install pre-commit
pre-commit install
git config pre-commit.ruby "rvm `rvm current` do ruby"
git config pre-commit.checks "[whitespace, pry, console_log, debugger, rubocop]"
@franzejr
franzejr / ruby_blocks.rb
Last active December 7, 2015 00:29
Ruby Blocks examples
## Execute Around
def time_it(label)
start_time = Time.now
yield
elapsed_time = Time.now - start_time
puts "#{label} took #{elapsed_time} seconds"
end
time_it("Sleepy code") do