APL-Inspired Python!

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024
  • 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/Dya...
    TryAPL: tryapl.org/
    allEqual APL Talk: • Algorithms as a Tool o...
    Follow me on Github: github.com/cod...
    Follow me on Twitter: / code_report
    Follow me on LinkedIn: / codereport

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

  • @biomechanoid
    @biomechanoid 3 ปีที่แล้ว +135

    APL is like what would happen if elves invented regular expressions

  • @cameronball3998
    @cameronball3998 2 ปีที่แล้ว +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 ปีที่แล้ว +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

    • @PhthaloJohnson
      @PhthaloJohnson 11 หลายเดือนก่อน

      @@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 11 หลายเดือนก่อน

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

    • @RedHair651
      @RedHair651 10 หลายเดือนก่อน

      ​@@PhthaloJohnsonPython has types, though, or maybe I'm misunderstanding.

    • @cory1111
      @cory1111 9 หลายเดือนก่อน

      @@PhthaloJohnsonScheme 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.

  • @clementdato6328
    @clementdato6328 2 ปีที่แล้ว +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.

  • @aDifferentJT
    @aDifferentJT 3 ปีที่แล้ว +35

    Haskell also encourages the same style of thinking

  • @liesdamnlies3372
    @liesdamnlies3372 3 ปีที่แล้ว +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.

  • @atrus3823
    @atrus3823 3 ปีที่แล้ว +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.

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

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

  • @Kruglord
    @Kruglord ปีที่แล้ว +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)))

  • @MartinClausen
    @MartinClausen 3 ปีที่แล้ว +7

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

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

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

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

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

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

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

  • @MrRyanroberson1
    @MrRyanroberson1 2 ปีที่แล้ว +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

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

    Love your channel man

  • @apdy27
    @apdy27 3 ปีที่แล้ว +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 10 หลายเดือนก่อน

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

  • @MichaelGrantPhD
    @MichaelGrantPhD 2 ปีที่แล้ว +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 2 ปีที่แล้ว +9

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

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

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

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

      github.com/interregna/arraylanguage-companies

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

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

    • @nanthilrodriguez
      @nanthilrodriguez 3 ปีที่แล้ว +9

      @@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.

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

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

    • @abrudz
      @abrudz 3 ปีที่แล้ว +7

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

    • @nanthilrodriguez
      @nanthilrodriguez 3 ปีที่แล้ว +4

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

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

    12:07 know ur libraries, functions, & algos

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

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

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

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

  • @Vogel42
    @Vogel42 3 ปีที่แล้ว

    dude, you sound different. are you ok?

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

      his microphone setup improved a lot - sounds perfectly clear now

  • @SayWhat6187
    @SayWhat6187 3 ปีที่แล้ว +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
    )

  • @mahmud-i9c
    @mahmud-i9c 2 ปีที่แล้ว

    aimfpl