great artists steal orion edwards m314 rubyscoping lambda swiftnullability c++zero cost abstraction...

Post on 12-Jan-2016

221 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Great Artists Steal

Orion Edwards M314

It comes down to trying to expose yourself to the best things that humans have done and then try to bring those things in to what you’re doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve Jobs

It comes down to trying to expose yourself to the best things that humans have done and then try to bring those things in to what you’re doing. I mean Picasso had a saying, he said good artists copy great artists steal.

Steve Jobs

Topics

Ruby Scoping Lambda

Swift Nullability

C++ Zero cost abstraction

Obj-C Naming

Grand Central Dispatch

Go Channels

Contact me @borland

Ruby – Scoping lambdaFile.open 'test.txt', 'w' do |f| f.write "Hello World 3"end

C# - Scoping lambdadatabase.Transaction(tx => { tx.Execute("delete from people where id = 5"); tx.Commit();});

DEMO

github.com/borland/Ignite2015

Resource you really don't want to leakLock, Transaction

Build up, then commitTemplates, Batching

Scoping lambda:

Things that don't need restricted lifetime(use IDisposable instead)

Not for:

Code is data

Lisp

canvas.Line(10, 10, 30, 30, true);

Naming

[canvas lineFromX:10 fromY:10 toX:30 toY:30 antialiased:true];

The method name islineFromX:fromY:toX:toY:antialiased:

Objective-C – naming

canvas.line(Color.Red, 30, 30, 100, 100);canvas.Line( fromX:30, fromY:30, toX:100, toY:100, antialiased:true);

C# – naming

Think about your parameter names as part of your method name, form a phrase

Swift – Optionalfunc readFile(path:String) -> String? { …}

if let contents = readFile("/path/to/file") {print(contents)

} else {print("no contents")

}

C# – OptionalOptional<string> ReadFile(string path) { …}

ReadFile("path/to/file").Unwrap( c => Console.WriteLine(c), () => Console.WriteLine("no contents"));

DEMO

github.com/borland/Ignite2015

Return type for methods that may return null

Optional:

Function input parameters (use naming instead) Person(string name, string optionalAddress);

Unwrap best for small things (use x.HasValue/Value otherwise)

Not for:

HtmlEncoded<string>

Atomic<bool>

Tainted<string>

Either<string, int>

This pattern generalizes:

C++ - Zero Cost Abstractiondouble x;

struct Metres { double Value;};

8 bytes in memory

Also 8 bytes in memory

C++ - Zero Cost Abstractiondouble x;

struct Metres { // has overloaded + operator double Value;};

double x = 100;double x2 = x + 100;

Metres m(100);Metres m2 = m + Metres(100);Call overloaded operator +

Gets inlined and optimised away

DEMO

You already use them (System.DateTime)

Multiple data types in combinationE.g. Distance / Speed

When unit of measure is importantCurrency

Wrapper structs:

Standalone params (use naming) void Delay(int timeoutMilliseconds);

Not for:

The compiler should prevent

you writing programs with

bugs

Haskell

Grand Central DispatchApple's solution for concurrency

Queues are "lightweight virtual threads"

You "dispatch" lambda functions onto them

System manages the queues

GCD – Main Queue

Dispatcher.BeginInvoke(() => { ShowAlert("hello");});

dispatch_async(dispatch_get_main_queue()) { showAlert("hello")}

C#

GCD – Concurrent Queues

Task.Run(() => { ProcessFile("path\\to\\file");});

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { processFile("path/to/file")}

C#

GCD – Serial Queues

let q = dispatch_create_queue("myq", nil)

dispatch_async(q) { processFile("path/to/file")}dispatch_sync(q) { flushFileBuffers()}

No builtin C# equivalent

Worker Thread 1

Worker Thread 2

Q1

Q1 Job1

Q1 Job2

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Worker Thread 1

Worker Thread 2

Q1

Q1 Job2

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job1

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2

Q3 Job1

Worker Thread 1

Worker Thread 2

Q1

Q1 Job3

Q2 Q3Serial QueueAsync Dispatch

Q1 Job2 Q3 Job1

If the queue is idle, it just locks, no thread switching

Else queues a request and waits for it

Serial queues are a lock that you can schedule things onto

Synchronous Dispatch

C# - Serial Queuesvar q = new SerialQueue();

q.DispatchAsync(() => {ProcessFile("path\\to\\file");

});

q.DispatchSync(FlushFileBuffers);

DEMO

github.com/borland/SerialQueue

Apple: everywhere instead of threadsTo manage an actual queue"upgrade" existing thread-based codeA simpler alternative to a thread or lock

Serial Queues:

Parallel work that doesn't share (use Task)

Basic Locking (use lock)

Not for:

Go – goroutines and channelsfunc main() { result := make(chan int) go multiply(10, 20, result) time.Sleep(1000 * time.Millisecond) fmt.Println("result was", <- result)}

func multiply(a int, b int, result chan int) {

result <- a * b}

Goroutine 1func main

Goroutine 2func multiply

result := make(chan int)go multiply(10, 20,result)

10 * 20Blocking Send to result channel

fmt.Println("off we go")

Read result channel

time.Sleep(…)

Println("result was", 200)

Goroutines are basically just Task.Run

Channels are interesting:send/receive allows message passing between tasks

Block the task for safetyBut not the OS thread, to achieve scalability

Has select to "switch" on multiple channels

Goroutines and channels:

Do not communicate by sharing memory;

instead, share memory by

communicating.

Go

C# – first takeTask<int> MultiplyAsync(int a, int b) => Task.Run(() => a * b); async void Main() { int result = await MultiplyAsync(10, 20); WriteLine($"result was{result}");}

func multiply( a int, b int, result chan int, messages chan string) { result <- a * b messages <- "ok"}

Doesn't scale:

Tuple<Task<int>, Task<string>> MultiplyAsync(int a, int b) { ????????????}

Doesn't scale:C#:

C# – Channels + awaitasync Task MultiplyAsync( int a, int b, Channel<int> result, Channel<string> messages){ await result.Send(a * b); await messages.Send("ok");}

DEMO

github.com/borland/Ignite2015

Async work which posts multiple resultsIf you like go If it helps you separate your concerns

Channels can work to enhance the TPL, not replace it

When to use Channels

Async work with a single result (just use Task)

Not for:

Never write finalizers in C#System.Runtime.InteropServices.SafeHandle classMicrosoft.Win32.SafeHandles namespace

System.Collections.ImmutableNuget package from the Microsoft .NET team

"Extension variables"ConditionalWeakTable class in System.Runtime.CompilerServices

Reactive ExtensionsRx-Main Nuget package from Microsoft for .NETRxcpp Nuget package for C++RxJS Nuget package for JavaScriptRxJava for Java

Honora

ble

M

entions

Related Ignite NZ Sessions

Torment Your Colleagues with the 'Roslyn' .NET Compiler PlatformNZ2 - Fri 1:55pm

What's new in Visual Studio 2015 and ALM 2015 – Part 2NZ2 - Wed 2:30pm

What's new in Visual Studio 2015 and ALM 2015 – Part 1NZ2 - Wed 1:55pm

TPL & async/await Best Practices for the Busy DevBallroom 1 - Wed 3:10pm

Find me later at… Hub Happy Hour Wed 5:30-6:30pm Hub Happy Hour Thu 5:30-6:30pm @borland on twitter

1

2

3

4

Complete your session evaluation now and win!

Expose yourself to great ideas and do

great things

© 2015 Microsoft Corporation. All rights reserved.Microsoft, Windows and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or

other countries.MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

top related