Skip to content

Instantly share code, notes, and snippets.

@SteveBate
SteveBate / Functions.js
Last active December 23, 2015 21:19
A Javascript exercise to create a revealing module that exposes linq-style functional operators such as map, reduce, filter and others. Note that most of these operations are available on the Array class anyway.
var utils = utils || {};
utils.linq = function(){
range = function(start, end){
var numbers = [];
for(var x=start; x<=end;x++)
numbers.push(x);
return numbers;
}
@SteveBate
SteveBate / ArrayExercise.js
Created October 9, 2013 13:36
Exercise testing the various Javascript Array methods but particularly the functional methods available
// set up arrays
var numbers = [1,12,4,18,9,7,11,3,101,5,6];
var strings = ['this','is','a','collection','of','words'];
// array.reduce - find largest number
var largestValue = numbers.reduce(function(x,y){ return x > y ? x : y });
console.log('largest number: ' + largestValue);
// array.reduce - find longest string
@SteveBate
SteveBate / PipeAndFilters.js
Last active December 25, 2015 02:19
A basic Javascript implementation of the messaging pipeline as described at http://eventuallyconsistent.net/2013/08/12/messaging-as-a-programming-model-part-1/
var utils = utils || {};
// pipeline
utils.pipeline = function(){
var handlers = [];
var register = function(handler){
if(typeof handler !== 'function')
throw { name: 'InvalidTypeException', description: 'handler should be a function'}
@SteveBate
SteveBate / PipeAndFilters.scala
Created October 9, 2013 13:56
A basic Scala implementation of the messaging pipeline as described at http://eventuallyconsistent.net/2013/08/12/messaging-as-a-programming-model-part-1/
import scala.collection.mutable.ListBuffer
val msg1 = new Network1(1);
val msg2 = new Network1(2);
val msg3 = new Network1(3);
val pipeline = new PipeLine[Network1]();
pipeline.register((x: Network1) => new GetUndeliveredOrders(x))
.register((x: Network1) => new GetDeliveryInfo(x))
.register((x: Network1) => new GetPodImage(x))
@SteveBate
SteveBate / Go routines.go
Created August 24, 2014 17:54
Example showing use of Waitgroups and with the time.After channel. Remove the "go" call to have all calls to UpdateSchedulesReport run sequentially otherwise they run concurrently
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var w sync.WaitGroup
@SteveBate
SteveBate / Task.cs
Last active August 29, 2015 14:12
A version of the pipe and filters task but with the ability to also specify middleware (aspects) at the same time.
void Main()
{
// create a pipeline task
var mytask = new Task<OrderMatrixMessage>();
// register aspects
mytask.Wrap(new LoggingAspect<OrderMatrixMessage>());
mytask.Wrap(new ExceptionLoggingAspect<OrderMatrixMessage>());
mytask.Wrap(new AsyncAspect<OrderMatrixMessage>());
@SteveBate
SteveBate / MapSort.go
Created January 10, 2015 16:25
Example of sorting a map via different sorting implementations
package main
import (
"fmt"
"sort"
)
type Person struct {
Title string
FirstName string
@SteveBate
SteveBate / FileWatching.go
Last active August 29, 2015 14:13
Sample of monitoring a log file for changes and filtering out lines that describe an error
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
@SteveBate
SteveBate / testingin.go
Last active August 29, 2015 14:16
Simplifying testing in Go via a few help functions
// Given a simple web server sample:
func main() {
config := "this is some config"
http.Handle("/", HandleDefaultRoute(config))
http.ListenAndServe(":8080", nil)
}
func HandleDefaultRoute(config string) http.HandlerFunc {
@SteveBate
SteveBate / async.cs
Last active August 29, 2015 14:18
Example of scraping urls asnchronously using async, await and tasks
async System.Threading.Tasks.Task Main()
{
await Get("http://bbc.co.uk").ContinueWith(async r => {
using(var resp = r.Result.GetResponseStream())
{
var content = new MemoryStream();
await resp.CopyToAsync(content);
var html = System.Text.Encoding.Default.GetString(content.ToArray());
html.Dump(); // linqpad
}