Instantly share code, notes, and snippets.

# Ben Ngben-ng

• Sort options
Created May 30, 2019
View companion-object.scala
 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 {
Created Mar 4, 2019
Reverse a linked list in C
View rev.c
 while (current != null) { next = current->next; current->next = prev; prev = current; current = next; } head = prev;
Created Mar 4, 2019
Reverse a linked list in Bash
View problem-statement.md

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

Created Mar 4, 2019
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)
Created Mar 4, 2019
FizzBuzz solved with data-flow programming in Bash.
View fizzbuzz.sh
 #!/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*//'
Created Sep 21, 2016
Sample solution for converting roman numerals to integers
View roman.py
 def convert(char): return { 'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1 }.get(char)
Created Nov 14, 2014
Use UIWebView's `loadHTMLString:baseURL:` and set the baseURL to where your assets are in the app bundle. Then replace all absolute paths in your html/js/css with relative ones using something like the script below. The relative URLs will be loaded relative to the baseURL, whereas absolute ones are loaded relative to the root of the filesystem, …
View fixPrefixes.m
 // This doodad converts absolute paths like "/assets/thing.jpg" into relative ones like "assets/thing.jpg" NSString * (^fixPrefix)(NSString *, NSString *) = ^NSString *(NSString * input, NSString *prefix) { NSString *needle = [[@"/" stringByAppendingString:prefix] stringByAppendingString:@"/"]; NSString *replacement = [prefix stringByAppendingString:@"/"]; input = [input stringByReplacingOccurrencesOfString:[@"'" stringByAppendingString:needle] withString:[@"'" stringByAppendingString:replacement]]; input = [input stringByReplacingOccurrencesOfString:[@"\"" stringByAppendingString:needle] withString:[@"\"" stringByAppendingString:replacement]]; input = [input stringByReplacingOccurrencesOfString:[@"(" stringByAppendingString:needle] withString:[@"(" stringByAppendingString:replacement]]; return input;
Last active Mar 29, 2017
substr shim
View substr.js
 // 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;
Last active Mar 29, 2017
Salesforce Marketing Cloud Function.length bug
View a-repro.js
 Platform.Load('Core', '1.0'); function moo (foo, bar) {}; Write(moo.length); // You'll get a .NET error if you do this, weirdly enough.
Last active Mar 28, 2017
AMD silently misbehaving on Salesforce Marketing Cloud
View a-repro.js
 define('foo', function () { function Foo () { Write('Hello World'); }; return Foo; }) require(['foo'], function (Foo) { // Foo is undefined on Salesforce
You can’t perform that action at this time.