Functional Programming with Kotlin • Hadi Hariri • GOTO 2018

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ต.ค. 2024

ความคิดเห็น • 45

  • @marcelmundl9012
    @marcelmundl9012 5 ปีที่แล้ว +5

    As for the last question: You can pretty easily accomplish that with a simple extension function:
    inline operator fun T.invoke(lambda: T.() -> Unit) = lambda(this)

    • @TroenderTass
      @TroenderTass 4 ปีที่แล้ว

      @Jon Do Haven't seen the ideo yet, but if you try to run that code. you gonna be dispointed. That lambda doesn't even take a param. Now, you could run that code without passing this in as a param, and rather use this.lambda()

  • @mehdimohammadi1623
    @mehdimohammadi1623 5 ปีที่แล้ว +4

    It was interesting especially the final part about sample DSL in Kotlin using extension function passing into higher-order function

  • @nasserabbassi7303
    @nasserabbassi7303 3 ปีที่แล้ว +1

    how life would be easier with functional paradigm . special thanks to Hadi Hariri

  • @damaddinm88
    @damaddinm88 6 ปีที่แล้ว +2

    Nice presentation. Basically everything what is new and about Kotlin is nice :)

  • @simplet3306
    @simplet3306 4 ปีที่แล้ว +1

    Simple and understandable. Thanks alot

  • @HudsonPereiradishark
    @HudsonPereiradishark 6 ปีที่แล้ว +2

    Great talk! Didn't know this Arrow KT library. Thank you!

  • @raymuko
    @raymuko 5 ปีที่แล้ว +1

    That DSL won't work unless you call f() from your invoke operator function. It should be: operator fun invoke(f: Configuration.() -> Unit) = f()

  • @damaddinm88
    @damaddinm88 6 ปีที่แล้ว +7

    As well one improvement idea. People like seeing code running :)

  • @jean4j_
    @jean4j_ 2 ปีที่แล้ว

    Is there an advantage to use Kotlin over Scala for functional programming though?
    This is a serious and honest question

    • @vibovitold
      @vibovitold ปีที่แล้ว

      I'm not an expert, but since you haven't received any better answer...
      Kotlin isn't as powerful for a functional language as Scala, it's missing some constructs such as union types, for example (and some can be just emulated).
      And this is by design, Kotlin is meant to remain a "friendly", hybrid language.
      On the other hand, Scala syntax can be intimidating and it's easier to write virtually incomprehensible code in Scala
      Kotlin is probably best suited for "diet" functional programming where you don't intend to take the paradigm all the way.
      So I guess the advantage of Kotlin over Scala, or vice versa, comes down to the technical background of the team, and the project requirements (full blown FP vs. FP-flavored)

  • @vibovitold
    @vibovitold 5 ปีที่แล้ว +7

    It's an interesting talk which could be better if it wasn't for some slip-ups. Let me share a few nitpicks
    3:11 While I agree with the general message, to be fair, your imperative implementation is obfuscated by poor naming as much as by its imperativeness. The "element" variable is misnamed. It clearly doesn't represent an element - an element is element[i]. Your "element" represents the number of occurrences of a given element, and the name should reflect that for the code to be readable. In my case this by itself was responsible for most of the delay in figuring out what the code does. Poor naming can be more of an obstacle than poor code structure.
    20:43 - inside the numbers.forEach, what is "numbers % 5 == 0" supposed to mean? You probably meant "it % 5 == 0"
    25:48 - putting const on a getter of Date type makes no sense at all. const can't have a getter. And const can't be used for non-primitive types (such as Date) either; strings being a sole exception. You haven't correctly indicated how the keyword works and what it is for, only created more confusion.
    37:25 - "this I can do in shorthand". But what you do then isn't a shorthand for what you had before at all. "this is the same as doing an if". But no, it's not the same. You don't have println. Just the value. And println(s?.length) - which is what someone might thought they'd need, relying on your words - would actually execute println with a null argument, if s were null. This isn't clear from your explanation. For the code to really do the same thing you'd need something like s?.length?.let(::println), or s?.run { println(length) } etc. This piece of the puzzle is missing.
    42:50 to 43:07 - I doubt if anyone who doesn't already know how it works from elsewhere actually understood the (non-)explanation...

  • @AnonimoslawAnonimowy
    @AnonimoslawAnonimowy 4 ปีที่แล้ว

    Nice presentation, but I haven't got what the last part about DSL capabilities of Kotlin have to do with functional programming. (except that functions are used for DSL)

  • @i.i
    @i.i 6 ปีที่แล้ว

    how about curring

  • @IUfidi
    @IUfidi 2 ปีที่แล้ว

    Thanks?, Hadi.

  • @gareginasatryan6761
    @gareginasatryan6761 4 ปีที่แล้ว +1

    Honest question. Why was Kotlin invented, when they already had Scala. Also why did Scala start losing out to Kotlin?

    • @_DATA_EXPUNGED_
      @_DATA_EXPUNGED_ 3 ปีที่แล้ว +2

      Because google decided so, i'd say.
      Additionally, the java-compatibility thing is quite important too, with kotlin you can without issues continue a java app in the new language. With scala you're going to have to be quite careful.
      Thus, kotlin has much easier access to the java ecosystem.

    • @jean4j_
      @jean4j_ 2 ปีที่แล้ว +1

      @@_DATA_EXPUNGED_ Also there are just way too many ways to do things and sometimes pretty exotic and fancy things (in a bad way when it comes to software development as a team). The Scala community kind of sucks too.
      I do like Scala. Some people consider Kotlin as sane Scala. I think that's one way to look at it :)

    • @vibovitold
      @vibovitold ปีที่แล้ว

      Kotlin's stronghold, and arguably the driving force behind its adoption, became Android, because developers were really stuck with Java, and unlike on backend, it wasn't even new versions of Java (unavailable, due to VM limitations).
      Scala never made a dent there - despite some attempts, it was never adopted for Android development.
      the notoriously long build times probably played a role (while one of the design goals of Kotlin was to be as fast as Java in this regard).
      and yes, another important issue is interoperability with Java. it's quite limited with Scala, while Kotlin is fully interoperable (some unavoidable quirks notwithstanding). this was again among Kotlin's chief design goals, and it's of particular importance on Android.
      finally - although this is subjective - Scala falls a bit on the "academic" side of languages, while Kotlin has a simpler, friendlier feel to it.

  • @piotreek7
    @piotreek7 5 ปีที่แล้ว

    What is the point of using Option, when Kotlin has it built in? All the examples shown in the presentation would be much shorter and more readable with Int? instead of Option.

    • @vibovitold
      @vibovitold 5 ปีที่แล้ว +1

      Kotlin's nullable types are nulls (for the sake of interoperability with Java). If you use a library that doesn't allow for nullability, like - most prominently - RxJava 2, then you can't send an instance of T? down the stream. It'll crash on a null in runtime. If you can't control the source of data, and/or refactoring the entire thing to remove nullability isn't easy, then you could use Optional as a value wrapper for "absent" values.
      Note the same problem doesn't exist in the - otherwise very similar - Swift, where the issue of Java interoperability obviously doesn't apply, and conversely, T? types in Swift are syntax sugar for Optionals.

  • @ossamabounnite4922
    @ossamabounnite4922 6 ปีที่แล้ว +1

    What's the programming language that starts with "b" and finishes with "k" which he was talking about?

    • @gregorykhvatsky7668
      @gregorykhvatsky7668 6 ปีที่แล้ว +8

      Well, maybe I didn't get your sarcasm/joke, but he was talking about Brainfuck

    • @mhdunknown
      @mhdunknown 2 ปีที่แล้ว +1

      Brainfuck.

    • @suhaib96
      @suhaib96 11 หลายเดือนก่อน +1

      Brainf*ck
      by Urban Müller

    • @suhaib96
      @suhaib96 11 หลายเดือนก่อน +1

      th-cam.com/video/hdHjjBS4cs8/w-d-xo.html

  • @MuhammadAlkady
    @MuhammadAlkady 6 ปีที่แล้ว

    Thanks

  • @zhangwei2671
    @zhangwei2671 5 ปีที่แล้ว +2

    so how to type the lambda symbol? :)

    • @natepepin09
      @natepepin09 5 ปีที่แล้ว +2

      In your font settings change it to a fira or some other programming font that has support and then enable font ligatures.

    • @lyrixtuber
      @lyrixtuber 5 ปีที่แล้ว +1

      copy and paste from wikipedia

    • @xpulse03
      @xpulse03 5 ปีที่แล้ว

      he added a live template which replaces the florin symbol (OPT+F on us-keyboard macs) with the lowercase lambda, but you can also use any other identifier, like "lambda"

  • @edgeeffect
    @edgeeffect 2 ปีที่แล้ว

    Nice slides.... completely impossible to read.

  • @dengan699
    @dengan699 5 ปีที่แล้ว

    According to him php and python are functional

  • @anthavio
    @anthavio 6 ปีที่แล้ว +3

    Please stop calling usage of lambdas functional programming. Even using datatypes from Arrow is not a functional programming.

    • @yasindiouri6088
      @yasindiouri6088 5 ปีที่แล้ว +4

      Please enlighten all of us about what is functional programming!

    • @heliobessonirodrigues6632
      @heliobessonirodrigues6632 5 ปีที่แล้ว +2

      It IS functional programming. But it's not a PURE functional language.

    • @okdoomer620
      @okdoomer620 5 ปีที่แล้ว +1

      We can call it "functional programming", but that kind of misses the point of using words. You can do this stuff in almost any language used today. I would argue, that the real difference is between pure functional languages like haskell, nix or elm, and all the other languages. Pure func. languages have referential transparency, that allow the compiler to make guarantees about the code, can give you hints, can make optimizations, and can execute it in the order it wants. To get this to work you can not have variable mutation and you need to carefully seperate side-effectful code from pure code. This distinction is not made in all the popular languages. And to complicate things further, there are even languages like scala, that allow you to optionally seperate these things, so it's the responsibility of the programmer to make it work, but the compiler doesn't really care. Mixing those things in Haskell would result in a compile time error.
      Maybe Hello Bessoni Rodrigues is right and we should just view "functional programming" as a style (independently from the language) where you just put maps and filters where you had for-loops previously, but that would be very different to what you get with pure functional programming, so using the term like this is kind of misleading, and the terms "functional programming" and "pure functional programming" would fall into to different categories.

    • @vibovitold
      @vibovitold 5 ปีที่แล้ว +1

      I agree. This is a bit like saying "I touched an object, therefore I do OOP". Imagine if this object is just a backet for some main() function, and within this function you're referring to Console or something, but it's all spaghetti, then yes you're technically dealing with some objects, but you're writing procedural code regardless :)