Skip to content

Instantly share code, notes, and snippets.

View chrisosaurus's full-sized avatar

Chris Hall chrisosaurus

View GitHub Profile
$ cat hello.c
int main(void){
if(1);
int i = 1;
$ gcc hello.c -std=c89 -pedantic
hello.c: In function ‘main’:
hello.c:3:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int i = 1;
^~~
@chrisosaurus
chrisosaurus / gist:3685bbd7bf0a41eef4a22c66d077865c
Created February 13, 2017 12:06
Profiling linear_hash against std::unordered_map
linear_hash
Profiling Set - repeating keys
10000000 set(s) took '1790' ms total or '0.179000' ns each
10000000 get(s) took '1711' ms total or '0.171100' ns each
10000000 exists(s) took '1621' ms total or '0.162100' ns each
Profiling Set - unique keys
35591 set(s) took '14' ms total or '0.393358' ns each
35591 get(s) took '7' ms total or '0.196679' ns each
A syntax I am playing with can be seen here; every reference is immutable by
default but mutability can be opted into using an ampersand (`&`):
# function that will add `1` to every mutable Signed Integer in a mutable List
fn add_one(&n::List[&Sint])
for &item in &n
&item += 1
end
end
@chrisosaurus
chrisosaurus / rust_mycell.rs
Last active January 13, 2017 02:07
Example implementing a quick and dirty 'Cell like' type 'MyCell'
// DO NOT USE THIS
// heavily derived from Rust's Cell
// https://github.com/rust-lang/rust/blob/master/src/libcore/cell.rs
struct MyCell<T> {
value: T
}
impl<T:Copy> MyCell<T> {
pub fn new(value: T) -> MyCell<T> {
@chrisosaurus
chrisosaurus / gist:8071277fb998e70ce17df9040e5aefc0
Last active January 6, 2017 06:51
Example showing Icarus integer type inference 'problems'
builtin fn minus(Sint, Sint) -> Sint
builtin fn divide(Sint, Sint) -> Sint
builtin fn print(Sint)
fn foo() -> Sint
# problem: what should the typing for these literals be ?
let a = 16
let b = 24
let c = a / b
print(c)
fn main()
let &l::List[&Sint8] = [1, 2, 3]
&l.append(4)
print(l)
end
# the type of T here *must* be a read only permission type
# therefore the typ of `l` in `print(l)` above must be ::List[Sint8]
fn print[T](l::List[T])
type Foo
b::Bar plus equals divide print
c::Baz multiply
end
@chrisosaurus
chrisosaurus / rust_immut.rs
Created January 3, 2017 05:18
Example showing interesting Rust immutable reference behaviour
use std::cell::Cell;
struct Point {
x: i32,
y: Cell<i32>,
}
/* mutate takes an *immutable* reference to a Point */
fn mutate(point: &Point){
point.y.set(1000);
@chrisosaurus
chrisosaurus / my_memmove.c
Last active December 29, 2016 08:12
(a failed) attempt at implementing memmove in C99 without any pointer -> integer conversions
#include <memory.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void *my_memmove(void *dest, const void *src, size_t n);
void *my_memmove(void *dest, const void *src, size_t n){
size_t i = 0;
size_t j = 0;
package main
import "fmt"
func adder(first int) func(int) int {
func inner(second int) int {
return first + second
}
return inner
}