Skip to content

Instantly share code, notes, and snippets.

View edvardm's full-sized avatar

Edvard Majakari edvardm

  • Rakettitiede Oy
  • Finland
  • 18:34 (UTC +03:00)
View GitHub Profile
SET g TO 0.05 * 9.81. // minmus
// SET g TO 0.166 * 9.81. // mun
SET f TO 50.
LOCK m TO SHIP:MASS.
LOCK aTot TO g - F/m.
LOCK tThrottle TO v0 / aTot.
SET LANDED_ALTITUDE TO 10.0.
# tallensin tämän, koska harva vitsi kuvaa yhtä hyvin sitä täsmällisyyttä,
# mitä matematiikassa vaaditaan :)
Insinööri, fyysikko ja matemaatikko olivat matkalla Skotlannissa.
Junasta he näkivät mustia lampaita laitumella.
"Hei, Skotlannissa lampaat ovat mustia!", huudahti insinööri.
- Ei, tiedämme vain, että ainakin jotkin lampaat Skotlannissa ovat mustia, totesi
fyysikko.
Viimeisenä matemaatikko ojensi molempia: "Väärin. Varmuudella voimme sanoa vain,
@edvardm
edvardm / psql-test.hs
Created October 24, 2014 13:32
test postgres adapter
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.Text as Text
import Database.PostgreSQL.Simple
import Control.Monad
import Control.Applicative
import Data.Maybe
queryStuff :: IO ()
rot13 = map (rot13' pairs) where
pairs = (zip s1 s2) ++ (zip s2 s1)
s1 = ['a'..'m'] ++ ['A'..'M'] ++ ['0'..'4']
s2 = ['n'..'z'] ++ ['N'..'Z'] ++ ['5'..'9']
rot13' [] ch = c
rot13' ((p,q):cs) c = if c == p then q else (rot13' cs c)
@edvardm
edvardm / break_after_middle.rb
Last active August 29, 2015 14:07
break string in the middle or after of the sentence with <br>
def break_after_middle(s)
midpoint = s.length/2-1
idx_after_middle = s[midpoint..-1].index(' ')
if idx_after_middle
idx = idx_after_middle + midpoint
s.dup.tap { |p| p[idx..idx] = '<br/>' }
else
s
end
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>esclient-sample</groupId>
<artifactId>esclient-sample</artifactId>
<version>1.0-SNAPSHOT</version>
@edvardm
edvardm / symbolize_recursive.rb
Last active May 22, 2020 04:17
Recursively symbolize ruby hash keys in a nested structure. Uses refinements instead of monkey patching Hash.
module SymbolizeHelper
extend self
def symbolize_recursive(hash)
{}.tap do |h|
hash.each { |key, value| h[key.to_sym] = transform(value) }
end
end
private
@edvardm
edvardm / batch_worker.rb
Last active December 29, 2015 12:59
Simple worker that does stuff in batches. Block passed to initializer should be a method that accepts list of things to work at a time
class BatchWorker
attr_reader :size
def initialize(size, &block)
@size = size
@buf = []
@block = block
end
def write(value)
@edvardm
edvardm / hash_lists.rb
Last active December 25, 2015 06:39
combine list of hashes
# merge_by_common_key(:k, [ { k: 1, a: 2 }, { k: 1, b: 3 }, { k: 2, c: 4} ])
# => [ { k: 1, a: 2, b: 3}, { k: 2, c: 4 } ]
def merge_by_common_key(key, list_of_hashes)
list_of_hashes.group_by { |h| h[key] }.map { |_, hs| fold_merge(hs) }
end
def fold_merge(hs)
hs.each_with_object({}) { |h, acc| acc.merge!(h) }
end
@edvardm
edvardm / rails_uniq_finder_extension.rb
Created September 6, 2013 09:13
I often need this in specs, sometimes I'd like to include this in code as well. The idea is to ensure that the query returns really only single result
module FinderExtensions
def find_uniq!(cond)
res = where(cond)
res.first.tap do |r|
raise ActiveRecord::RecordNotFound unless r
raise ActiveRecord::ActiveRecordError, "multiple records found" if res[1]
end
end
end