2016-11-12 02 Николай Линкер. Чему java может поучиться у haskell и...
TRANSCRIPT
WHATWHATWHATWHATWHATWHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHATWHATJAVACANLEARNFROM
HASKELLHASKELLHASKELLHASKELLHASKELLHASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELLHASKELLANDVICEVERSA
WHATWHATWHATWHATWHATWHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHAT
WHATWHATJAVACANLEARNFROM
HASKELLHASKELLHASKELLHASKELLHASKELLHASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELL
HASKELLHASKELLANDVICEVERSA
Nick Linker @nick_linkerNick Linker @nick_linker
EXAMPLES( )
Anchor Applikativ Assertible Borders Beautiful_DestinationsAnchor Applikativ Assertible Borders Beautiful_DestinationsByteAlly Capital_Match ByteAlly Capital_Match Chordify CircuitHub CommonwealthChordify CircuitHub Commonwealth
Bank DigitalX Elsen Extensibl Facebook FP_CompleteBank DigitalX Elsen Extensibl Facebook FP_CompleteFPInsight Front_Row_Education Helium_SystemsFPInsight Front_Row_Education Helium_SystemsHooky,_Inc Infinipool Iris_Connect Hooky,_Inc Infinipool Iris_Connect Keera_StudiosKeera_Studios
Kite_&_Lightning Least_Fixed LexisNexis_Risk_SolutionsKite_&_Lightning Least_Fixed LexisNexis_Risk_SolutionsLumi_Guide Lumi_Guide Madriska_Inc. Microsoft MidrollMadriska_Inc. Microsoft Midroll
MyFansDemand Picus_Security Pivot_Cloud PreziMyFansDemand Picus_Security Pivot_Cloud PreziRheo_Systems Scoompa Scrive Scyfy_Technologies SilkRheo_Systems Scoompa Scrive Scyfy_Technologies SilkSimplyRETS Snowdrift.coop SimplyRETS Snowdrift.coop Soostone Stack_BuildersSoostone Stack_BuildersStandard_Chartered Stitcher Suite_Solutions SumAllStandard_Chartered Stitcher Suite_Solutions SumAll
Swift_Navigation Systor_Vest thoughtbot Tree.isSwift_Navigation Systor_Vest thoughtbot Tree.isTsuru_Capital Turing_Jump Tsuru_Capital Turing_Jump UpHere VaryWell VFILESUpHere VaryWell VFILESVirtual_Forge Wagon Wellposed Well-Typed ZaloreVirtual_Forge Wagon Wellposed Well-Typed Zalore
SOURCE
HASKELLIS...FunctionalPureLazy (by default)With advanced type systemGHC
25 years old, but moves fastlast release 2016-05-21
WHATJAVACANLEARNExpressive syntaxPurityExpressive Type SystemGHCi (REPL)...
EXPRESSIVESYNTAX
EXPRESSIVESYNTAX(1)sizexs=loopxs0whereloop[]acc=accloop(_:xs)acc=loopxs(acc+1)
--Usagesize[1,2,3]
EXPRESSIVESYNTAX(2)
The type of size aboveThe type of size above
In generalIn general
size::[t]->Integer--?
size::Numa=>[t]->a--actualltype
f::(C1a)=>a->b->c->d->e(fa1)::b->c->d->e(fa1b1)::c->d->e(fa1b1c1)::d->e(fa1b1c1d1)::e
EXPRESSIVESYNTAX(3)
A spherical program in vacuumA spherical program in vacuum
moduleMy.Foowhere
importData.Timehiding(Day)
foo::IO()foo=doct<-getCurrentTimeputStrLn("UTCtime="++showct)
PURITY
PURITY(1)
Seriously, what about file system? Network? Random?Seriously, what about file system? Network? Random?
PURITY(2)
They have the same Java type.They have the same Java type.
However, these functions are not interchangeable!However, these functions are not interchangeable!
longgetLength(Stringstr){returnstr.length();}
longgetFileLength(Stringpath){returnnewFile(path).length();}
PURITY(3)
In Haskell they'd have different types:In Haskell they'd have different types:
(Monad tutorial goes here...)(Monad tutorial goes here...)
getLength::String->Integer
getFileLength::String->IOInteger
EXPRESSIVETYPES
EXPRESSIVETYPES(1)
NEWTYPES
Milliseconds vs secondsUsername vs passwordPaths vs contentsIndices
In Haskell wrapping can be free!In Haskell wrapping can be free!
//callthisasrunScript("sql/RunStuff.sql")ResultrunScript(Stringscript){...}
EXPRESSIVETYPES(2)
NEWTYPES
Looks like a separate type, but low-level representation isLooks like a separate type, but low-level representation isthe same.the same.
--typesaferunScriptnewtypePath=PathString
runScript::Path->IOResult
EXPRESSIVETYPES(3)
ALGEBRAICDATATYPESANDPATTERNMATCHINGdataVoiddataX=XdataY=YIntTextXdataZ=ZxX|ZyYdataDay=Mon|Tue|Wed|Thu|Fri|Sat|SundataUser=User{id::Int,name::Text,day::Day}
EXPRESSIVETYPES(4)
ALGEBRAICDATATYPESANDPATTERNMATCHING
Constructing values and matchingConstructing values and matching
letz=Zy(Y123"Hey"X)letu1=User{id=1,name="Vasya",day=Mon}letu2=User2"Petya"Tueletd=Sat
wd::Day->Stringwdd|d`elem`[Mon,Tue,Wed,Thu]->"Workingday"wdd|d`elem`[Sat,Sun]->"Weekendday"wdFri->"Friday"
caseu1ofUseridnameday->...
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple
Orange
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple Eq
Orange
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple Eq Ord
Orange
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple Eq Ord
Orange Eq
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple Eq Ord
Orange Eq ToJSON
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(5)
TYPECLASSES
Typeclasses decouple the declaration that a typeTypeclasses decouple the declaration that a typeimplements an interface from the declaration of the typeimplements an interface from the declaration of the type
itselfitself
DataType Eq Ord ToJSON FromJSON
Apple Eq Ord
Orange Eq ToJSON
Lemon Eq Ord ToJSON FromJSON
More on More on Intefaces vs TypeclassesIntefaces vs Typeclasses
EXPRESSIVETYPES(6)
TYPECLASSES
EXPRESSIVETYPES(7)
ITISPOSSIBLETOCHECKATCOMPILETIME
Arrays boundsOpen vs closed filesNested transactionsGuaranteed closing resourcesREST endpointsAndtestareavailabletoo!
ANDMANYMORE
WHATHASKELLCANLEARN
Flat learning curveIntellij IDEAStackoverflow...
HASKELLLEARNINGCURVE
LEARNINGCURVE(1)
HASKELLLEARNINGCURVE,COMPARISON
LEARNINGCURVE(2)
THINGSTOLEARNINHASKELL
1. Syntax, functions from Prelude2. Monads3. Concurrency & parallelism, STM4. Libraries
1. Monad transformers2. Free monads, recursive schemes3. Arrows, Lens, Type families4. Type safe DSLs, TH5. Whateveryouwant
LEARNINGCURVE(4)
COMPAREWITHC++
Thenewbookreleased,thetranslationofThenewbookreleased,thetranslationoftheC++17StandardDrafttoRussian.888theC++17StandardDrafttoRussian.888
pages.Yousay,Haskellistoocomplex?pages.Yousay,Haskellistoocomplex?Okay...Okay...@dshevchenko@dshevchenko
INTELLIJIDEA
There is no analog for Haskell.There is no analog for Haskell.
There are plugins/extensions forThere are plugins/extensions for
AtomVimEmacsSublime
However, Haskell's stepping debugger (GHCi) is notHowever, Haskell's stepping debugger (GHCi) is notuniversal.universal.
STACKOVERFLOWANDDOCS
For Java it is easy to find examples and goodFor Java it is easy to find examples and gooddocumentation.documentation.
For HaskellFor Haskell
The documentation is often poorNeeded to look into the libraries' codeFortunately I could ask my colleagues directly.
OURCASE
OURCASE
1. There were communication problems inside the team2. There was a split between haskellers and javaists3. ...Despite the pretty good quality of the services itself4. The productivity would not save us :-/
OURCASE
1. There were communication problems inside the team2. There was a split between haskellers and javaists3. ...Despite the pretty good quality of the services itself4. The productivity would not save us :-/
One should take the social aspects into account One should take the social aspects into account during theduring theintroduction of the new technologies.introduction of the new technologies.
SO?
ISITWORTHTOUSEHASKELLINPRODUCTION?
1. Only if all team members are eager to learn Haskell2. Maybe for some separate task, e.g. compiler3. I believe one can grow a team of Haskellers4. .. but cannot easy switch team to Haskell (you cannot
force people to learn)
Examples: GHC, Corrode, Elm, PureScript, Agda,Examples: GHC, Corrode, Elm, PureScript, Agda,Kaleidoscope, PandocKaleidoscope, Pandoc
Haskell is not for Production and Other Tales, Katie MillerHaskell is not for Production and Other Tales, Katie Miller and and VideoVideo SlidesSlides
ISITWORTHTOLEARNHASKELL?(1)
YES!1. to get a new way of thinking and to push the boundaries2. to know how to structure things without inheritance3. to know the alternatives to the buzzwords: DDD,
Anemic, Patterns, IOC, SOLID, DI, ...4. to finally understand monads
ISITWORTHTOLEARNHASKELL?(2)
OO is full of design problemsOO is full of design problems
is hard to get it done righta lot of buzzwordsas opposite to algorithms there is no clear criteriawhether is one solution better than anothercow.eat(grass)grass.beEatenBy(cow)field.eatingInteraction(cow,grass)
ISITWORTHTOLEARNHASKELL?(2)
QUESTIONS?