Skip to content

Instantly share code, notes, and snippets.

object Foo {
var initialized = 0
// This only happens once regardless of how many times Foo.fetch() is called
initialized = initialized + 1
def fetch(): Integer = initialized
}
class Foo {
@ben-ng
ben-ng / rev.c
Created March 4, 2019 06:06
Reverse a linked list in C
while (current != null) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
@ben-ng
ben-ng / problem-statement.md
Created March 4, 2019 06:03
Reverse a linked list in Bash

Given an input.txt that represents a linked list a -> b -> c -> d -> e as a series of nodes with next pointers:

a>b
b>c
c>d
d>e

Reverse the linked list such that the output reads:

@ben-ng
ben-ng / reverse-linked-list.sh
Created March 4, 2019 06:02
Solution for reversing a linked list in bash
#!/usr/bin/env bash
# Where input.txt is:
# a>b
# b>c
# c>d
# d>e
paste -d '>' <(< input.txt | cut -d'>' -f2 | tac) <(< input.txt | cut -d'>' -f1 | tac)
@ben-ng
ben-ng / fizzbuzz.sh
Created March 4, 2019 05:39
FizzBuzz solved with data-flow programming in Bash.
#!/usr/bin/env bash
seq 1 100 | \
xargs -Ix -n1 echo "x + ((x % 3 == 0) + ((x % 5) == 0) * 2) * 1000" | \
bc | \
xargs -n1 printf "%04d\n" | \
sed 's/^1.*/Fizz/' | \
sed 's/^2.*/Buzz/' | \
sed 's/^3.*/FizzBuzz/' | \
sed 's/^0*//'
@ben-ng
ben-ng / substr.js
Last active March 29, 2017 18:28
substr shim
// There is no String.substr on Salesforce. This uses String.substring instead.
// substring's arguments are different, so this just converts substr args to
// their substring equivalents.
function substrShim (str, start, len) {
if (len <= 0)
return '';
if (start < 0)
start = str.length + start;
@ben-ng
ben-ng / a-repro.js
Last active March 28, 2017 21:01
AMD silently misbehaving on Salesforce Marketing Cloud
define('foo', function () {
function Foo () {
Write('Hello World');
};
return Foo;
})
require(['foo'], function (Foo) {
// Foo is undefined on Salesforce
@ben-ng
ben-ng / a-repro.js
Last active March 29, 2017 18:27
Salesforce Marketing Cloud Function.length bug
Platform.Load('Core', '1.0');
function moo (foo, bar) {};
Write(moo.length); // You'll get a .NET error if you do this, weirdly enough.
@ben-ng
ben-ng / a-repro.js
Created March 24, 2017 18:32
Salesforce Marketing Cloud constructor-from-closure bug
function newEnv () {
function Foo () {
Write('Hello World');
};
var f = {
Foo: Foo
}
return f;
@ben-ng
ben-ng / a-repro.js
Last active March 24, 2017 18:20
Salesforce Marketing Cloud closure scope bug
function foo() {
function boom() {
Write('Boom');
};
function bar () {
function baz() {
boom(); // Fails here, "boom" can't be found
};