Skip to content

Instantly share code, notes, and snippets.

View bew's full-sized avatar
😎
https://nohello.net

Benoit de Chezelles bew

😎
https://nohello.net
View GitHub Profile
@bew
bew / keymap.c
Created February 10, 2017 12:00 — forked from pocketkk/keymap.c
Chord and Pick Keymap
// This is the canonical layout file for the Quantum project. If you want to add another keyboard,
// this is the style you want to emulate.
#include "planck.h"
#ifdef BACKLIGHT_ENABLE
#include "backlight.h"
#endif
// Each layer gets a name for readability, which is then used in the keymap matrix below.
// The underscores don't mean anything - you can have a layer called STUFF or any other name.
@bew
bew / json_mapper.cr
Created March 13, 2017 04:52
A JSON.mapper for crystal that allow additional fields
module JSON
# JSON.mapping documentation removed, as it's taking to much spaces :p
# prop: the properties, like JSON.mapping
# decl: whatever or not to generate the variables declarations, getters & setters
# more_init: if not nil, must be a TupleLiteral that is the list of additional fields to the initialize method
macro mapper(props properties, strict = false, decl gen_declarations = true, more_init = nil)
{% for key, value in properties %}
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
@bew
bew / material_colors_hexs.json
Created March 18, 2017 18:35
All material colors in hex
[
{
"name": "red",
"key": "red",
"android": "red",
"default": {
"strength": 500,
"hex": "#f44336",
"contrast": "white"
},
@bew
bew / OOM error handling.cr
Last active May 5, 2017 01:22
Test for OutOfMemory exception
Kilobyte = 1024u32
Megabyte = 1024u32 * Kilobyte
Gigabyte = 1024u32 * Megabyte
def write_debug(str)
LibC.write 1, str, str.size
end
class FatalOOMError < Exception
def initialize
@bew
bew / rpc_call_concept.cr
Last active August 12, 2017 01:24
Cool micro RPC fake calls with object serialization, deserialization, method call
module SharedNode
ADDRESS_PROXY = {} of Int32 => {UInt64, Node.class}
@@last_id = 0
def self.add_node(node)
@@last_id += 1
ADDRESS_PROXY[@@last_id] = {node.as(Void*).address, node.class}
@@last_id
end
module Contract
# We put everything in `included` macro so the constants don't clashes
# when used in multiple classes
macro included
CONTRACTS_FOR_DEF = {} of _ => _
macro __add_contract(contract)
\{% if CONTRACTS_FOR_DEF[:next_def] == nil %}
\{% CONTRACTS_FOR_DEF[:next_def] = [contract] %}
\{% else %}
\{% CONTRACTS_FOR_DEF[:next_def] << contract %}
// Super Fast Blur v1.1
// by Mario Klingemann <http://incubator.quasimondo.com>
//
// Tip: Multiple invovations of this filter with a small
// radius will approximate a gaussian blur quite well.
//
BImage a;
BImage b;
void setup()
@bew
bew / 5_combinators.cr
Created February 15, 2018 01:32
Functional programming in Crystal to implement parser combinators
# Following: https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
require "spec"
# Result types
record Success(T), value : T, remaining : String do
def_equals_and_hash value, remaining
end
@bew
bew / literal_expander_in_macros.cr
Last active May 25, 2018 20:50
ArrayLiteral expansion using macros only
# Based on https://github.com/crystal-lang/crystal/blob/26635f1052b99ab5b52a8b051020a25435dba751/src/compiler/crystal/semantic/literal_expander.cr
macro expand_literal_ArrayLiteral_empty(node)
{% puts "--- expansion for: #{node}" %}
Array({{ node.of }}).new
{% debug %}
end
@bew
bew / manual_class.cr
Created September 21, 2018 20:07
Manually create a class in Crystal, from existing memory
#macro data(name, &block)
#end
#
# #### Ultimately I want to do this:
# and be able to make a pre-allocated buffer of Person,
# and be able to get a reference to a single Person (but pre-allocated
# in that buffer)
#
#data Person do
# property name : String