Skip to content

Instantly share code, notes, and snippets.

@bcardiff
bcardiff / README.md
Created April 8, 2016 08:59
sample-data-confirm

This sample show how to attach globally to events over dynamically created elements. And how can data-* attributes be used to customize behaviour.

def ej(a, b)
size = [a.size, b.size].min
a[0...size].zip(b[0...size])
.map { |e| e.max }
.map(&.+(4))
.select(&.>=(10))
.sum
end
ej [1,5,3,4], [5,3,6]
data = [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# unstable
data.sort_by &.[0] # => [{"Brian", 42}, {"Alan", 30}, {"Brian", 32}]
# stable
data.map_with_index { |d,i| {d,i} }.sort_by { |di| {di[0][0], di[1]} }.map { |di| di[0] } # => [{"Alan", 30}, {"Brian", 42}, {"Brian", 32}]
@bcardiff
bcardiff / match.cr
Last active March 17, 2016 15:31
benchmark for headers matching
require "benchmark"
def string_contains_word(source, word)
return false unless source
start = source.index(word)
return false unless start
return false if start > 0 && source[start-1].alphanumeric?
return false if start + word.size < source.size && source[start + word.size].alphanumeric?
true
end
puts "sfs"
class Crystal::Playground::Agent
def sendp(t)
send(t)
end
end
# $p.try &.sendp("bug")
10.times do |i|
puts i
puts "sfs"
10.times do |i|
puts i
{ "Lon\nrem", i, i + 1 }
sleep 1
end
sleep 2
@bcardiff
bcardiff / triangles in logo
Created March 10, 2016 18:35
logo program to draw triangles. Use http://www.calormen.com/jslogo/
@bcardiff
bcardiff / mtry.cr
Created February 18, 2016 14:58
mtry macro for crystal
# sample of a macro that flood the method calls with `#try`
# as a POC it does not support methods with arguments
macro mtry(expr)
{% if expr.is_a?(Call) %}
mtry({{expr.receiver}}).try(&.{{expr.name}})
{% else %}
{{expr}}
{% end %}
end
@bcardiff
bcardiff / README.md
Last active February 19, 2022 15:13
hex with alpha in p5js
@bcardiff
bcardiff / README.md
Last active November 17, 2015 01:06
p5e1

INTRODUCTION TO PROGRAMMING FOR THE VISUAL ARTS WITH P5.JS

Assignment 1: Port an Image to Code