Skip to content

Instantly share code, notes, and snippets.

View berkus's full-sized avatar
🎯
Layers of obstruction

Berkus Decker berkus

🎯
Layers of obstruction
View GitHub Profile
@berkus
berkus / AnalogLiterals.hpp
Created September 13, 2021 21:34 — forked from yamamushi/AnalogLiterals.hpp
Multi-Dimensional Analog Literals in C++
/*
Referenced from:
http://web.archive.org/web/20120110153227/http://weegen.home.xs4all.nl/eelis/analogliterals.xhtml
*/
#ifndef ANALOGLITERALS_HPP
#define ANALOGLITERALS_HPP
namespace analog_literals {
@berkus
berkus / playground.rs
Created August 29, 2019 07:34 — forked from rust-play/playground.rs
Code shared from the Rust Playground
#![feature(unboxed_closures, fn_traits)]
fn main() {
let add = |a: i32, b: i32| a + b;
let sqr = |a: i32| a.pow(2);
let add_two = |a: i32| a + 2;
assert_eq!(chain(add, sqr)(2, 3), 25);
assert_eq!(
chain(
@berkus
berkus / playground.rs
Created August 2, 2019 15:24 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::collections::hash_map::Entry;
use std::collections::HashMap;
trait EntryExt<'a, K, V>: 'a {
fn or_insert_with2<F: FnOnce(&K) -> V>(self, f: F) -> &'a mut V;
}
impl<'a, K, V> EntryExt<'a, K, V> for Entry<'a, K, V> {
fn or_insert_with2<F: FnOnce(&K) -> V>(self, f: F) -> &'a mut V {
match self {
@berkus
berkus / playground.rs
Created May 5, 2019 09:47 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait Def<T> {
fn def(&self, foo: T) -> bool;
}
impl<T, Func> Def<T> for Func
where Func: Fn(T) -> bool
{
fn def(&self, foo: T) -> bool {
(self)(foo)
@berkus
berkus / playground.rs
Last active April 16, 2019 16:34 — forked from rust-play/playground.rs
Transmute UB and `as` UB
#![allow(mutable_transmutes)]
use std::mem::transmute;
fn main() {
let a: Vec<u64> = Vec::new();
let r = &a;
let r: &mut Vec<u64> = unsafe { transmute(r) };
r.push(1488);
println!("{:?}", a);
@berkus
berkus / xbox-one-wireless-protocol.md
Created April 4, 2019 13:21 — forked from alfredkrohmer/xbox-one-wireless-protocol.md
XBox One Wireless Controller Protocol

Physical layer

The dongle itself is sending out data using 802.11a (5 GHz WiFi) with OFDM and 6 Mbit/s data rate:

Radiotap Header v0, Length 38
    Header revision: 0
    Header pad: 0
    Header length: 38
    Present flags
@berkus
berkus / wasmRustNodeExample.md
Created April 4, 2019 06:48 — forked from ilyakmet/wasmRustNodeExample.md
WASM Rust to Node Example

WASM Rust to Node Example

Use only > 8.x.x NodeJS version

Install Rust before using this tutorial: curl https://sh.rustup.rs -sSf | sh

Create dirs:

mkdir wasmRustNodeExample

cd wasmRustNodeExample
@berkus
berkus / as_trait.rs
Last active March 29, 2019 16:12 — forked from rust-play/playground.rs
Returning Box<dyn Self> via helper trait
trait Trait: AsTrait {
fn get_answer(&self) -> i32;
}
struct S;
impl Trait for S {
fn get_answer(&self) -> i32 { 42 }
}
@berkus
berkus / playground.rs
Created January 14, 2019 08:02 — forked from rust-play/playground.rs
Code shared from the Rust Playground
#![feature(result_map_or_else)]
extern crate serde; // 1.0.84
extern crate serde_derive; // 1.0.84
extern crate serde_json; // 1.0.34
use serde::Serialize;
#[derive(Debug, Serialize)]
#[serde(tag = "status", content = "result")]

1. Separation of immutable and mutable logic

Quite a lot of different people have been on the same trail of thought. Gary Bernhardt's formulation of a "functional core, imperative shell" seems to be the most voiced.

"Boundaries" - Gary Bernhardt

"Imperative shell" that wraps and uses your "functional core".. The result of this is that the shell has fewer paths, but more dependencies. The core contains no dependencies, but encapsulates the different logic paths. So we’re encapsulating dependencies on one side, and business logic on the other side. Or put another way, the way to figure out the separation is by doing as much as you can without mutation, and then encapsulating the mutation separately. Functional core — Many fast unit tests. Imperative shell — Few integration tests

https://www.youtube.com/watch?v=yTkzNHF6rMs