APL-Inspired Python!

Sdílet
Vložit
  • čas přidán 20. 07. 2021
  • In this video, I first solve a LeetCode problem in APL and then solve it in Python in a similar fashion.
    Dyalog APL: www.dyalog.com/
    Dyalog RIDE: github.com/Dyalog/ride
    TryAPL: tryapl.org/
    allEqual APL Talk: • Algorithms as a Tool o...
    Follow me on Github: github.com/codereport
    Follow me on Twitter: / code_report
    Follow me on LinkedIn: / codereport
  • Věda a technologie

Komentáře • 53

  • @biomechanoid
    @biomechanoid Před 2 lety +135

    APL is like what would happen if elves invented regular expressions

  • @clementdato6328
    @clementdato6328 Před 2 lety +8

    I don’t know there is a language APL, but only the language “APL which is my favourite programming language” after watching all of your videos.

  • @cameronball3998
    @cameronball3998 Před 2 lety +34

    I’m very new to APL, but use Python as my main language. This channel is really inspiring me to get into functional programming over OOP. To say my jaw dropped when I saw the final one-liner was an understatement-that solution is just beautiful. I can’t get enough of functional programming now.

    • @sebastiangudino9377
      @sebastiangudino9377 Před rokem +1

      A little distinction is that what you saw here is not necessarily "Functional" programing. It's declarative programing. Those terms are often interchangeable. But python doesn't have great support for "Functional" programing. But it does excel at Declarative programing thanks to modules like itertools

    • @user-uf4rx5ih3v
      @user-uf4rx5ih3v Před 8 měsíci

      @@sebastiangudino9377 Declarative and functional are not interchangeable at all. Functional programming is declarative, but just because something is declarative does not make it functional. In particular, I would argue that in the absence of types, a language cannot be considered functional.

    • @sebastiangudino9377
      @sebastiangudino9377 Před 8 měsíci

      @@user-uf4rx5ih3v indeed, we are saying the same thing! Did you misread my comment?

    • @RedHair651
      @RedHair651 Před 7 měsíci

      ​@@user-uf4rx5ih3vPython has types, though, or maybe I'm misunderstanding.

    • @cory1111
      @cory1111 Před 6 měsíci

      @@user-uf4rx5ih3vScheme and Common Lisp barely have types. Would you consider them to be functional? Yes. Because the only true requirement of a functional language is a first class functions. Nothing about a type system or mutability or declarativity makes a language functional. In scheme there is barely any distinction between any type. Including functions and non-functions because they share the same namespace. Look at lambda calc. No type system there. More to it, a language can be functional and immutable and declarative and have a large type system like Haskell, but those 4 do not need to go hand in hand. Use Emacs lisp for any period of time and you’ll find that although you are mutating like crazy, the language is still functional. Functions are constantly being passed into functions, lambdas are everywhere, etc.

  • @atrus3823
    @atrus3823 Před 2 lety +21

    Python 3.10 has a new itertool called pairwise. You can mimic all_equal with all(starmap(eq, pairwise(ls))). Not super terse, but pretty cool. The downside of this is you need to use a lambda or list comprehension to use it in takewhile.
    Another idea: instead of a comprehension, I think it's nicer to use next(zip(*takewhile(...))) to get ('f', 'l'). A final solution would look something like:
    "".join(next(zip(*takewhile(lambda l: all(starmap(eq, pairwise(l))), zip(*words)))))
    Not the shortest, but no indexing, sequences, comprehensions, or external libs.

  • @liesdamnlies3372
    @liesdamnlies3372 Před 2 lety +10

    I hadn’t seen any APL before today, and I don’t consider myself much of a programmer either, but I was happy that the first solution to the problem that jumped into my head was exactly what you did in APL, moving-around and reshaping the data. It certainly has its uses.
    Glad you pointed-out to just use the builtins though. That really is Python’s biggest selling point to me. Lots and lots of builtins that are written in C by big-brain experts so they go really freakin’ fast. It’s like Python itself is high-level glue between a bunch of specialized pieces.

  • @aDifferentJT
    @aDifferentJT Před 2 lety +35

    Haskell also encourages the same style of thinking

  • @kasvith
    @kasvith Před rokem

    Love your channel man

  • @Kruglord
    @Kruglord Před rokem +2

    One trick, you might want to use next(x, None) instead of x[0], because if one of the words is an empty string, you'll get an index error using the direct indexing. If you just used next(x), you'd get a StopIteration exception, so including None as an argument lets you use that as the default value instead.
    Edit: Nevermind, apparently tuples aren't iterable
    In that case the best I can do is:
    ''.join(x[0] if len(x) > 0 else None for x in takewhile(all_equal, zip(*words)))

  • @arraymac227
    @arraymac227 Před 2 lety +6

    allowing imports in leetcode means all solutions reduce to: SolveLeetCode(ContestNo,Input)

  • @MartinClausen
    @MartinClausen Před 2 lety +7

    I came up with this in Clojure: (apply str (map first (take-while #(apply = %) (apply map (fn [& args] args) words))))

    • @MartinClausen
      @MartinClausen Před 2 lety +3

      Perhaps more readable:
      (->>
      words
      (apply map (fn [& args] args))
      (take-while #(apply = %))
      (map first)
      (apply str))

    • @MartinClausen
      @MartinClausen Před 2 lety +6

      A smidgen cleaner:
      (->>
      words
      (apply map vector)
      (take-while #(apply = %))
      (map first)
      (apply str))

  • @MrRyanroberson1
    @MrRyanroberson1 Před 2 lety +2

    10:38 You could have also done zip(*) again, even if it would be less efficient, and take the first element, an interesting symmetry with APL

  • @paulzupan3732
    @paulzupan3732 Před rokem +1

    Did you know that APL is Code Report’s favorite language?

  • @apdy27
    @apdy27 Před 2 lety +1

    Not clean but a two liner could be:
    predicate = (all(x == y[0] for x in y) for y in zip(*strs))
    return ''.join(x[0] for x in takewhile(lambda t: t[1], zip(strs[0], predicate)))

  • @rito_ghosh
    @rito_ghosh Před 6 měsíci

    How do I get the box around array elements? Which settings in RIDE do I need to change?

  • @yash1152
    @yash1152 Před 9 měsíci

    12:07 know ur libraries, functions, & algos

  • @MichaelGrantPhD
    @MichaelGrantPhD Před 2 lety +2

    I don't think your Python solution implemented the scan portion. So if there is a TTFTF pattern, for instance, what happens?

    • @MichaelGrantPhD
      @MichaelGrantPhD Před 2 lety +9

      Answered my own question: takewhile terminates at the first False.

  • @TNeulaender
    @TNeulaender Před 2 lety +3

    So. dumb question, but how do I type those chars on cli gnu-apl?

    • @abrudz
      @abrudz Před 2 lety +7

      Not dumb at all. Have a look at apl.wiki/Typing_glyphs

    • @nanthilrodriguez
      @nanthilrodriguez Před 2 lety +4

      Realize that the dyalog keyboards use a different character for ⋄ than gnu-apl-mode in emacs. Took many hours debugging to realize that

  • @gowtham5095
    @gowtham5095 Před 2 lety +5

    Which companies are still using APL language? Pls answer my query guys !

    • @code_report
      @code_report  Před 2 lety +5

      github.com/interregna/arraylanguage-companies

    • @gowtham5095
      @gowtham5095 Před 2 lety +1

      APL developer jobs are less right now it's right to learn now ! Any suggestions

    • @nanthilrodriguez
      @nanthilrodriguez Před 2 lety +8

      @@gowtham5095 Most companies that use APL are private about their use of APL so as not to advertise to their competitors. Best way to find a job using APL is to join the community forums, chat rooms, and get to know professionals.

  • @travoroguna7866
    @travoroguna7866 Před 2 lety +2

    Python solution without imports
    "".join(y.pop() for x in zip(*words) if len((y := set(x))) == 1)

    • @LeonidKuralin
      @LeonidKuralin Před rokem +1

      This is very elegant but not working. Try ["flower", "alow", "zlight"]

  • @Vogel42
    @Vogel42 Před 2 lety

    dude, you sound different. are you ok?

    • @CostaKazistov
      @CostaKazistov Před 2 lety +6

      his microphone setup improved a lot - sounds perfectly clear now

  • @noomade
    @noomade Před 2 lety +2

    I know I sound like a broken record, but please don't forget to do more Haskell stuff :)
    _(head ) . takeWhile ((all . (==) . head) tail) . transpose_

  • @SayWhat6187
    @SayWhat6187 Před 2 lety +1

    This is how I did it:
    from itertools import accumulate
    from functools import reduce
    reduce(
    lambda x, y: x[
    :len([
    1
    for e in accumulate(x, lambda i, j: i+j)
    if y.startswith(e)
    ])
    ],
    strs
    )

  • @shahratinmahmud
    @shahratinmahmud Před 2 lety

    aimfpl