Skip to content

Instantly share code, notes, and snippets.

@kaisellgren
kaisellgren / remove_duplicates.rs
Last active Aug 29, 2015
Removes duplicate entries from Vec with a complexity of O(N(N+1)/2).
View remove_duplicates.rs
/// Is based on the dual pointer technique where ´current´ iterates as usual,
/// but ´runner´ iterates until it hits the ´current´, and then ´current´ proceeds.
pub fn remove_duplicates<'a, T: Eq>(data: &'a mut Vec<T>) {
let mut current_index = 0;
while current_index < data.len() {
let mut runner_index = 0;
while runner_index < current_index {
if &data[runner_index] == &data[current_index] {
data.remove(current_index);
View fold.scala
def kaiFold[A, B](stuff: Seq[A], initial: B)(predicate: (B, A) => B): B = {
@tailrec
def it(items: Seq[A], accumulator: B): B = {
if (items.length == 0) accumulator
else {
val nextAccumulator = predicate(accumulator, items.head)
it(items.tail, nextAccumulator)
}
}
@kaisellgren
kaisellgren / Signal.scala
Last active Jan 7, 2016
Academic Scala implementations of Functional Reactive Programming -- different Signal[A] and foldp[A, B] implementations as examples
View Signal.scala
// This first Signal[A] impl. uses STM (Ref — transactional references).
class Signal[A](initial: A) {
type SignalListener = Function[A, Unit]
val value = Ref(initial)
val listeners = Ref(Seq[SignalListener]())
def set(newValue: A): Unit = {
// Atomically change the value within a transaction, blocks the thread, but a not an issue since it's a simple operation.
@kaisellgren
kaisellgren / gist:5000511
Last active Dec 14, 2015
Dart and problematic catching of exceptions. The exception isn't caught.
View gist:5000511
import 'dart:io';
import 'dart:async';
import 'dart:math';
// The following function is part of a third-party package that you can't touch.
Future doCoolStuff() {
var completer = new Completer();
// Sometimes bad things happen.
if (new Random().nextBool()) {
@kaisellgren
kaisellgren / app.js
Created Apr 21, 2012
Frog module for NodeJS
View app.js
// Include the Frog compiler.
require('./../lib/Frog.js');
// Now just run Dart like a boss.
console.log('I am about to run some Dart...');
require('./test.dart');
console.log('I just ran Dart!');
View styles.xaml
<Window.Resources>
...
<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
View cantwait.js
let {readFile, readFileYield} = require('fs');
let {spawn} = require('taskjs');
// Typical
readFile('foo', function(err, data) {});
// Exotic
spawn(function*() {
var data = yield readFileYield('foo');
// :-)
@kaisellgren
kaisellgren / Cin.event.EventPublisher.js
Created Dec 11, 2011
Cindela UI drafting, brainstorming
View Cin.event.EventPublisher.js
class EventPublisher {
// ...
/**
* Fires the given event.
* @param {String} name
*/
fire(name) {
// ...
}
View app.js
var settingsButton = SomeLib.createButton('#button-open-settings', {
disabled: true,
listeners: {
click: function(e) {
var dialog = SomeLib.createDialog('#settings-dialog');
dialog.show();
}
}
});