SlideShare a Scribd company logo
Mercury:
A Functional Review
Retrospective on a first commercial F# project
Mark Cheeseman
mcheesemanau@gmail.com
http://hombredequeso.id.au/
Why Use F# ?
• Less Code
• Easier to reason about the code
• Easier to multi-thread.
• You get to say “monad”
Why Did We Use F# : Immutability
By default, F# Record Types are immutable
type Point = {
X: int
Y: int
}
class Point
{
Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; private set; }
public int Y { get; private set; }
}
Why Did We Use F#: Value Equality
F# records have structural (value) equality.
Classes have reference equality by default, but can implement value equality.
type Point = {
X: int
Y: int
}
public class Point : IEquatable<Point>
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; private set; }
public int Y { get; private set; }
public bool Equals(Point other)
{
…
public class Point : IEquatable<Point>
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; private set; }
public int Y { get; private set; }
public bool Equals(Point other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return X == other.X && Y == other.Y;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Point) obj);
}
public override int GetHashCode()
{
unchecked
{
return (X*397) ^ Y;
}
}
public static bool operator ==(Point left, Point right)
{
return Equals(left, right);
}
public static bool operator !=(Point left, Point right)
{
return !Equals(left, right);
}
}
Why Did We Use F#:
Functional is the New SOLID
(or the end/goal of SI at least)
public interface IWidgetFactory
{
Widget Create(T1 t1, T2 t2);
}
http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/
Base
Type1 Type2
Base
Type1 Type2
BaseA
Type1 Type2
BaseB
Inheritance Composition
Refactored Composition
Why Did We Use F#:
Emergence of Functional Style with
Composition over Inheritance
Major Challenges
• Libraries/Frameworks to use
• Getting the most out of F# (idiomatic F#)
• How to put together a project
Local Server
Polling(WCF)
Shipment Polling
Endpoint
(C#)
External Shipping
System
Azure Web
Website
WebJob
Shipment
Processing
Endpoint
Bus
Shipment Polling DB
Shipment DB
(SQL Azure)
Mercury System Overview
Putting the Pieces Together
Requirement Technological Solution
Distributed Communication between company
server and Azure
NServiceBus
Db Access (SQL Azure) F# (Sql) Dbml Type Provider
Website (Azure) Nancy
Unit Testing FsUnit, FsCheck
Browser Aurelia
Website Security BrockAllen.MembershipReboot, IdentityServer3
class Shipment
{
public void Depart(T1 d)
{
// mutate _state
}
public void Arrive(T2 a)
{
// mutate _state
}
private State _state
}
type Shipment {
// state of shipment: record type
}
type Operation =
Depart of T1
Arrive of T2
let depart (d: T1) (s: Shipment) : Shipment =
// departs shipment s, and returns a new
// instance of Shipment in departed state
let arrive (a: T2) (s: Shipment) : Shipment =
// arrives shipment s, and returns a new
// instance of Shipmente in arrived state
let apply (operation: Operation) (shipment: Shipment)
: Shipment =
match operation with
| Depart(d) -> depart d shipment
| Arrive(a) -> arrive a shipment
Domain Code: C# and F#
Domain
Shipment DB (SQL Azure)
DAL
Composition Root
Bus (NSB)
NServiceBus Handlers
Nsb to Domain Transforms
Domain
Shipment DB (SQL Azure)
DAL
Composition Root
type Shipment
type Operation
apply:
Shipment -> Operation -> Shipment
… other entities
Bus (NSB)
NServiceBus Handlers
(ICmd -> unit)
Nsb to Domain Transforms
ICmd -> ShipmentEntity.Operation
Sql Type Provider
type dbContext.Shipment
type Dal.Shipment
dbContext.Shipment -> Dal.Shipment ->
Domain.Shipment
C# syntax
int GetHashCode(string s)
{
...
}
F# syntax
let GetHashCode (s: string) : int =
...
or just
let GetHashCode s =
...
F# function signature
GetHashCode: string -> int
Domain
Shipment DB (SQL Azure)
DAL
Composition Root
type Shipment
type Operation
apply:
Shipment -> Operation -> Shipment
(ShipmentEntity.fs)
… other entities
Bus (NSB)
NServiceBus Handlers
(ICmd -> unit)
NsbHandlers.fs
Nsb to Domain Transforms
ICmd -> ShipmentEntity.Operation
(NsbToDomainTransform.fs)
Sql Type Provider
type dbContext.Shipment
type Dal.Shipment
dbContext.Shipment -> Dal.Shipment ->
Domain.Shipment
ShipmentViewModelDal.fs
Pattern Hints
• Making illegal states unrepresentable with single case unions.
http://fsharpforfunandprofit.com/posts/designing-with-types-making-
illegal-states-unrepresentable/
• Do not throw exceptions.
http://fsharpforfunandprofit.com/rop/
Was it worth it?
Er, it depends…
On what?
Resources
http://fsharpforfunandprofit.com
One of the best, practically oriented F# resources.
http://blog.ploeh.dk/
Mark Seeman’s blog. Regularly blogs on F#.
https://www.youtube.com/watch?v=MHvr71T_LZw
Domain-Driven Design, Event Sourcing and CQRS with F# and
EventStore
Syme et. al., Expert F# 4.0
Petricek, Real-World Functional Programming With Examples in F# and C#

More Related Content

What's hot

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
 
built in function
built in functionbuilt in function
built in function
kinzasaeed4
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
Mark Whitaker
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
Minimal standard c program
Minimal standard c programMinimal standard c program
Minimal standard c program
Swain Loda
 
Function overloading
Function overloadingFunction overloading
Function overloading
Ashish Kelwa
 
Domain specific languages - progressive f sharp tutorials nyc 2012
Domain specific languages - progressive f sharp tutorials nyc 2012Domain specific languages - progressive f sharp tutorials nyc 2012
Domain specific languages - progressive f sharp tutorials nyc 2012
Phillip Trelford
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
Hadziq Fabroyir
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
HalaiHansaika
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
Tomas Petricek
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
Roman Okolovich
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
Fisnik Doko
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 

What's hot (20)

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
built in function
built in functionbuilt in function
built in function
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Minimal standard c program
Minimal standard c programMinimal standard c program
Minimal standard c program
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Domain specific languages - progressive f sharp tutorials nyc 2012
Domain specific languages - progressive f sharp tutorials nyc 2012Domain specific languages - progressive f sharp tutorials nyc 2012
Domain specific languages - progressive f sharp tutorials nyc 2012
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
basics of c++
basics of c++basics of c++
basics of c++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 

Similar to Mercury: A Functional Review

Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
Luka Jacobowitz
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
Eelco Visser
 
C# 6
C# 6C# 6
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
Alexander Varwijk
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
Satish Verma
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
ChengHui Weng
 
PyData NYC 2019
PyData NYC 2019PyData NYC 2019
PyData NYC 2019
Li Jin
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
STX Next
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
Free Based DSLs for Distributed Compute Engines
Free Based DSLs for Distributed Compute EnginesFree Based DSLs for Distributed Compute Engines
Free Based DSLs for Distributed Compute Engines
Joydeep Banik Roy
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
Jamie (Taka) Wang
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
Garth Gilmour
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
Garth Gilmour
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#
Talbott Crowell
 
GoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPHGoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPH
Gautam Rege
 
Programming Languages: some news for the last N years
Programming Languages: some news for the last N yearsProgramming Languages: some news for the last N years
Programming Languages: some news for the last N years
Ruslan Shevchenko
 

Similar to Mercury: A Functional Review (20)

Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
C# 6
C# 6C# 6
C# 6
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
PyData NYC 2019
PyData NYC 2019PyData NYC 2019
PyData NYC 2019
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Free Based DSLs for Distributed Compute Engines
Free Based DSLs for Distributed Compute EnginesFree Based DSLs for Distributed Compute Engines
Free Based DSLs for Distributed Compute Engines
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#
 
GoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPHGoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPH
 
Programming Languages: some news for the last N years
Programming Languages: some news for the last N yearsProgramming Languages: some news for the last N years
Programming Languages: some news for the last N years
 

Recently uploaded

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 

Recently uploaded (20)

National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 

Mercury: A Functional Review

  • 1. Mercury: A Functional Review Retrospective on a first commercial F# project Mark Cheeseman mcheesemanau@gmail.com http://hombredequeso.id.au/
  • 2. Why Use F# ? • Less Code • Easier to reason about the code • Easier to multi-thread. • You get to say “monad”
  • 3. Why Did We Use F# : Immutability By default, F# Record Types are immutable type Point = { X: int Y: int } class Point { Point(int x, int y) { X = x; Y = y; } public int X { get; private set; } public int Y { get; private set; } }
  • 4. Why Did We Use F#: Value Equality F# records have structural (value) equality. Classes have reference equality by default, but can implement value equality. type Point = { X: int Y: int } public class Point : IEquatable<Point> { public Point(int x, int y) { X = x; Y = y; } public int X { get; private set; } public int Y { get; private set; } public bool Equals(Point other) { …
  • 5. public class Point : IEquatable<Point> { public Point(int x, int y) { X = x; Y = y; } public int X { get; private set; } public int Y { get; private set; } public bool Equals(Point other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return X == other.X && Y == other.Y; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals((Point) obj); } public override int GetHashCode() { unchecked { return (X*397) ^ Y; } } public static bool operator ==(Point left, Point right) { return Equals(left, right); } public static bool operator !=(Point left, Point right) { return !Equals(left, right); } }
  • 6. Why Did We Use F#: Functional is the New SOLID (or the end/goal of SI at least) public interface IWidgetFactory { Widget Create(T1 t1, T2 t2); } http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/
  • 7. Base Type1 Type2 Base Type1 Type2 BaseA Type1 Type2 BaseB Inheritance Composition Refactored Composition Why Did We Use F#: Emergence of Functional Style with Composition over Inheritance
  • 8. Major Challenges • Libraries/Frameworks to use • Getting the most out of F# (idiomatic F#) • How to put together a project
  • 9. Local Server Polling(WCF) Shipment Polling Endpoint (C#) External Shipping System Azure Web Website WebJob Shipment Processing Endpoint Bus Shipment Polling DB Shipment DB (SQL Azure) Mercury System Overview
  • 10. Putting the Pieces Together Requirement Technological Solution Distributed Communication between company server and Azure NServiceBus Db Access (SQL Azure) F# (Sql) Dbml Type Provider Website (Azure) Nancy Unit Testing FsUnit, FsCheck Browser Aurelia Website Security BrockAllen.MembershipReboot, IdentityServer3
  • 11.
  • 12. class Shipment { public void Depart(T1 d) { // mutate _state } public void Arrive(T2 a) { // mutate _state } private State _state } type Shipment { // state of shipment: record type } type Operation = Depart of T1 Arrive of T2 let depart (d: T1) (s: Shipment) : Shipment = // departs shipment s, and returns a new // instance of Shipment in departed state let arrive (a: T2) (s: Shipment) : Shipment = // arrives shipment s, and returns a new // instance of Shipmente in arrived state let apply (operation: Operation) (shipment: Shipment) : Shipment = match operation with | Depart(d) -> depart d shipment | Arrive(a) -> arrive a shipment Domain Code: C# and F#
  • 13. Domain Shipment DB (SQL Azure) DAL Composition Root Bus (NSB) NServiceBus Handlers Nsb to Domain Transforms
  • 14. Domain Shipment DB (SQL Azure) DAL Composition Root type Shipment type Operation apply: Shipment -> Operation -> Shipment … other entities Bus (NSB) NServiceBus Handlers (ICmd -> unit) Nsb to Domain Transforms ICmd -> ShipmentEntity.Operation Sql Type Provider type dbContext.Shipment type Dal.Shipment dbContext.Shipment -> Dal.Shipment -> Domain.Shipment C# syntax int GetHashCode(string s) { ... } F# syntax let GetHashCode (s: string) : int = ... or just let GetHashCode s = ... F# function signature GetHashCode: string -> int
  • 15. Domain Shipment DB (SQL Azure) DAL Composition Root type Shipment type Operation apply: Shipment -> Operation -> Shipment (ShipmentEntity.fs) … other entities Bus (NSB) NServiceBus Handlers (ICmd -> unit) NsbHandlers.fs Nsb to Domain Transforms ICmd -> ShipmentEntity.Operation (NsbToDomainTransform.fs) Sql Type Provider type dbContext.Shipment type Dal.Shipment dbContext.Shipment -> Dal.Shipment -> Domain.Shipment ShipmentViewModelDal.fs
  • 16. Pattern Hints • Making illegal states unrepresentable with single case unions. http://fsharpforfunandprofit.com/posts/designing-with-types-making- illegal-states-unrepresentable/ • Do not throw exceptions. http://fsharpforfunandprofit.com/rop/
  • 17. Was it worth it? Er, it depends… On what?
  • 18. Resources http://fsharpforfunandprofit.com One of the best, practically oriented F# resources. http://blog.ploeh.dk/ Mark Seeman’s blog. Regularly blogs on F#. https://www.youtube.com/watch?v=MHvr71T_LZw Domain-Driven Design, Event Sourcing and CQRS with F# and EventStore Syme et. al., Expert F# 4.0 Petricek, Real-World Functional Programming With Examples in F# and C#

Editor's Notes

  1. Some blog posts related to this talk on the blog. Why we were interested in using F# (and why you might also be interested) What were some of the practical problems in actually getting an F# project underway. Was it worth it?
  2. Some commonly given reasons.
  3. What were OUR major reaons. Style of programming: use of value objects (immutable with value equality) event store like approaches
  4. Generated by Resharper of course… Let’s not get diverted by the less code = better issue But: code is communication, and there is a lot of code here to communicate something (and a lot of code to go wrong).
  5. SOLID Single Responsibility a class should only have one responsibility Open/Closed Liskov substitution Interface Segregation a class should not be dependent on anything more than necessary Dependency Inversion (e.g. a class with single responsibility of persistence or reading/writing to a db, say a repository, one responsibility, could give it two interface roles, read, and write) Emergence of smaller classes with less methods (but more classes) Static methods (trying to dream up static class names when a namespace would suffice) Linq chains (with static methods). Mark Seeman: Role based interface/classes (ISP) following the SRP push in the direction of an interface with one method.
  6. As composition is used more often, the reduction of specifically object-oriented features further reduces the need to be using an object oriented language Rule of thumb: use composition over inheritance. Some guidelines: do not use inheritance simply because there are common set of properties between objects, unless those properties truly reflect a common model item. do not use inheritance simply to gain access to common functionality required by two objects. carefully consider whether you really need inheritance if you are not mutating common base state with common methods, or as directed by common methods. if you are only using common base state, this state can still be provided as function parameters.
  7. How do I get my distributed, website-front end project which must persist something into Azure, and using webjobs? How do I go from that elegent F# code I keep seeing, with all the promise of no-null land and easy to reason about, to a product? How do I go from that immutable, pure functional business, into a product which inevitably mutates something (usually the db, but inevitably some user output, unless you want a completely inert product).
  8. Database Access (SQL) Distributed Communication (Bus) Website Unit/Integration Testing
  9. Pros use familiar frameworks in dev and production (along with familiar prod. tooling) Can pick up F# libraries as one feels comfortable Cons: C# interop code tends to be non-functional, can be messy, and is amongst the first code written May need C# to F# anti-corruption layer between outer parts of program (where frameworks interact) and core domain code to get the full benefits of F# Need to think about 2 styles of F#: purely functional, object-oriented/imperative. Try to keep them separate, but does put another burden on learning.
  10. Domain code: pure functional F#
  11. Main blocks of code. Nature of code in each block can be quite different.
  12. (bit on right: C#/F# syntax explanation) Composition Root: Includes some framework code (e.g. Nsb UnitOfWork) Includes minimal use of IOC container, mostly because we were using Nsb and Nancy. Most composition occurred within the handlers, or the top level Get/Post functions on the nancy module. Keep C# interop code separate from F# Consider anti-corruption against C# Single Responsibility functions will tend to fall naturally into composable layers – keep the responsibilities separated. How does one mutate the database : i.e. mutate the db based on an immutable domain.
  13. PROJECT LAYOUT- NOTABLE FEATURES: No directories Typically what would go in multiple C# files goes in one F# file. F# files must be in dependency order (NO CYCLIC DEPENDENCIES)
  14. Single Case Unions: equivalent of forcing use of a constructor. Exceptions are not functional. Just don’t
  15. Cons: Slower development time Whether a significant factor another issue. You are learning a new language, that comes at some cost. Team will inevitably be at different stages of F# usage (kind of like other languages, but even more exaggerated) C# interop: yuck. Pros: Benefits of immutability, value equality, functional push towards single responsibility, composition No nulls. Easier to refactor (I think). The strong alliance between types and functions make moving things around a whole lot easier. No cyclic dependencies: yes really it is a good thing. Easier to reason about code. It will make you a better programmer. Choose your paradigm (functional/OO) and stick with it. Either paradigm can be done in F# or C# Both paradigms can easily coexist in one program (preferably just not together). Focus on which one – the code as communication will be clearer.