Skip to content

Instantly share code, notes, and snippets.

View SKoschnicke's full-sized avatar

Sven Koschnicke SKoschnicke

View GitHub Profile
@SKoschnicke
SKoschnicke / consistency_problem.rb
Created January 26, 2010 12:03
Concurrency problems with Rails' ActiveRecord
class A < ActiveRecord::Base
# has attribute "value"
end
class B < ActiveRecord::Base
has_many :as
import System.IO
import Numeric(showIntAtBase)
import Char(ord, intToDigit)
main :: IO ()
main = do
output stdin
printFile :: String -> IO ()
printFile filename =
@SKoschnicke
SKoschnicke / bar.rb
Created August 24, 2012 07:48
Rails habtm extend
class Bar < ActiveRecord::Base
attr_accessible :rec_type
end
@SKoschnicke
SKoschnicke / gist:3917956
Created October 19, 2012 12:17
JQuery append only accepts real JS arrays, not JQuery-objects representing arrays (as map returns). Solution: Convert to JS array with makeArray.
$('<tr></tr>').append(
$.makeArray($(data).map(function(index, element) {
return $('<td></td>').html(element.instrument_name)
}))
).appendTo(custom_factor_table)
$('<tr></tr>').append(
$.makeArray($(data).map(function(index, element) {
return $('<td></td>').html('bla')
}))
).appendTo(custom_factor_table)
@SKoschnicke
SKoschnicke / error.hs
Created November 6, 2012 12:41
Elegant Haskell error handling
import Control.Error
-- A made-up type for this example.
-- This is a bit simplified because mostly there are not only these encapsuled
-- types but dependent values which can only be retrieved sequentially (e.g.
-- reading an id from stdin, then retrieving the record for this id from a
-- database).
type MyType = (Maybe (Maybe Int))
-- The goal: Return the int when it is less than 10, on other cases (greater or
@SKoschnicke
SKoschnicke / ExampleForSO.hs
Created June 17, 2013 09:56
Source for StackOverflow Question: Change element of list if it holds against some condition or add a new one if not, using Data.Lens (http://stackoverflow.com/questions/17087663)
{-# LANGUAGE TemplateHaskell, Rank2Types #-}
module ExampleForSO where
import Control.Lens
import Control.Applicative ((<$>), pure)
import Data.List (any)
data SomeRec = SomeRec { _name :: String, _val :: Int }
deriving (Show)
$(makeLenses ''SomeRec)
<?php
class Test {
private $number = 0;
private $sub;
public function __construct($n) {
$this->number = $n;
$this->sub = new SubTest($n, $this);
echo "construct ".$this->number."\n";
@SKoschnicke
SKoschnicke / merge.php
Last active August 29, 2015 13:57
Merge an array into an array of arrays, appending values to the array if the key exists or creating a new array with the value if not.
<?php
$a = array(1 => array(1),2 => array(2),3 => array(3), 12 => array(5));
$b = array(1 => 5,2 => 6,3 => 7, 5 => 12);
$result = $a + array_fill_keys(array_keys($b), []);
foreach ($b as $key => $value) {
array_push($result[$key], $value);
}
import Reactive.Banana
import Control.Concurrent
main = do
-- Create handles for pushing values into events
(addHandler1, push1) <- newAddHandler
(addHandler2, push2) <- newAddHandler
-- Create event network
network <- compile $ do
-- Actual events from AddHandlers
@SKoschnicke
SKoschnicke / keybase.md
Created September 18, 2014 10:35
keybase.md

Keybase proof

I hereby claim:

  • I am SKoschnicke on github.
  • I am skoschnicke (https://keybase.io/skoschnicke) on keybase.
  • I have a public key whose fingerprint is 00F0 5868 1412 6596 C9FF ACFB A408 2AE0 90DE 5CE9

To claim this, I am signing this object: