Skip to content

Instantly share code, notes, and snippets.

@SteveBate
SteveBate / linq.scala
Last active March 19, 2025 04:53
Scala LINQ equivalents
// http://stackoverflow.com/questions/8104846/chart-of-ienumerable-linq-equivalents-in-scala
xs.Aggregate(accumFunc) -> xs.reduceLeft(accumFunc)
xs.Aggregate(seed, accumFunc) -> xs.foldLeft(seed)(accumFunc)
xs.Aggregate(seed, accumFunc, trans) -> trans(xs.foldLeft(seed)(accumFunc))
xs.All(pred) -> xs.forall(pred)
xs.Any() -> xs.nonEmpty
xs.Any(pred) -> xs.exists(pred)
xs.AsEnumerable() -> xs.asTraversable // roughly
xs.Average() -> xs.sum / xs.length
@SteveBate
SteveBate / actors.scala
Created June 29, 2015 14:07
Example of two Akka actors interacting in Scala
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
case class RequestMessage(req: String)
case class ResponseMessage(resp: String)
class ClientActor extends Actor {
def receive = {
case ResponseMessage(resp: String) => println(resp)
@SteveBate
SteveBate / Task.scala
Last active August 29, 2015 14:23
Implementation of my C# Task (pipe and filters) in Scala
import scala.collection.mutable.ListBuffer
object HelloWorld {
def Start() {
println("OnStart")
}
def Complete() {
println("OnComplete")
@SteveBate
SteveBate / async_database.cs
Created March 30, 2015 13:22
Example of connecting to Sql Server using async, await and tasks
async void Main()
{
var cnn = new SqlConnection("Data Source=ERPDBUAT;Initial Catalog=Enterprise_Misc; Integrated Security=true");
cnn.Open();
var cmd = new SqlCommand("SELECT * FROM ShopService.DivisionRules WHERE DivCode = 'MHL001'", cnn);
await cmd.ExecuteReaderAsync().ContinueWith(async rdr => {
await rdr.Result.ReadAsync().ContinueWith(ok => {
rdr.Result.GetBoolean(rdr.Result.GetOrdinal("IsMobileEnabled")).Dump();
rdr.Result.GetBoolean(rdr.Result.GetOrdinal("IsWebEnabled")).Dump();
@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
}
@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 / 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 / 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 / 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 / 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