Skip to content

Instantly share code, notes, and snippets.

@barosl
barosl / prime-naive.rs
Created August 2, 2015 20:57
Naive prime number generation in Rust
//! Conforms to the input data format of http://www.spoj.com/problems/PRIME1/ but not usable as a
//! solution due to the slowness of the algorithm
use std::io::{stdin, BufRead};
use std::error::Error;
fn get_line<R: BufRead>(r: &mut R) -> Result<String, Box<Error>> {
let mut line = String::new();
try!(r.read_line(&mut line));
Ok(line)
@barosl
barosl / add.c
Created July 26, 2015 07:26
Function overloading in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addi(int a, int b) {
return a + b;
}
char *adds(char *a, char *b) {
char *res = malloc(strlen(a) + strlen(b) + 1);
@barosl
barosl / main.rs
Created July 7, 2015 00:17
Implicit coercion rules in Rust
// A normal function that accepts a `&str`
fn a(_: &str) {}
// A normal function that accepts a *trait* that is implemented by `&str`
trait StringNeeded {}
impl<'a> StringNeeded for &'a str {}
fn b<X: StringNeeded>(_: X) {}
@barosl
barosl / Traceback
Created April 12, 2015 21:44
This... is... so Java
scala.MatchError: 123 (of class java.lang.Integer)
at App$.main(test.scala:4)
at App.main(test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:70)
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:101)
@barosl
barosl / gist:1d3a9f2b6959d32ea2ca
Created January 7, 2015 20:42
Unscientific benchmarks of programming languages
Unscientific benchmarks using https://github.com/barosl/kong
C (optimized): 1.1s
Rust (optimized): 1.2s
C: 1.6s
Java: 3.7s
Haskell (optimized): 5.5s
Rust: 6.6s
JavaScript (with Node.JS): 9.3s
PyPy: 9.9s
@barosl
barosl / gist:0c2cff4755144a7d372d
Last active August 29, 2015 14:07
Decoding a hex string
>>> bytes.fromhex('ab')
b'\xab'
>>> import binascii
>>> binascii.unhexlify('ab')
b'\xab'
>>> import codecs
>>> codecs.decode('ab', 'hex')
b'\xab'
<meta name="viewport" content="width=device-width">
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.js"></script>
<style>
#in { width: 500px; }
</style>
<input type="text" id="in">
<div id="out"></div>
<script>
sock = new WebSocket('ws://baro.sl:7942/wakana');
//sock = new WebSocket('ws://baro.sl:8765/wakana');
@barosl
barosl / gist:2760954
Created May 21, 2012 07:33
Simple Exif manipulation script
HOURS_DIFFERENCE = 9 # can be negative
import os
import time
import datetime
import pyexiv2
for fname in os.listdir('.'):
if fname.lower().endswith('.jpg'):
print fname