Skip to content

Instantly share code, notes, and snippets.

@kaisellgren
kaisellgren / remove_duplicates.rs
Last active August 29, 2015 14:12
Removes duplicate entries from Vec with a complexity of O(N(N+1)/2).
/// 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);
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 January 7, 2016 03:03
Academic Scala implementations of Functional Reactive Programming -- different Signal[A] and foldp[A, B] implementations as examples
// 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.
<Window.Resources>
...
<!--A Style that affects all TextBlocks-->
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>