Python vs APL (1 Problem)

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 มิ.ย. 2021
  • In this video, I solve one problem in both Python and APL using the same solution.
    Problem Link: leetcode.com/contest/weekly-c...
    Dyalog APL: www.dyalog.com/
    Dyalog RIDE: github.com/Dyalog/ride
    Follow me on Github: github.com/codereport
    Follow me on Twitter: / code_report
    Follow me on LinkedIn: / codereport
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Gotta admire a language capable of doing so much in so few characters. Terrifying to look at, yes, but still remarkable.

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

      It's like regex for arrays and matrices

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

    Very nice. This inspired me to do the same in Clojure. Since you sometimes have Clojure on your channel I thought I'd share:
    (def s '("abc" "aabc" "bcccc"))
    (->> s
    (apply str)
    (frequencies)
    (vals)
    (map #(mod % (count s)))
    (every? zero?))

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

    I was having a good day until I learned about APL

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

      Same here bro

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

      Lol

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

      it just made me more excited, can't wait to learn it and bqn too

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

      Same, now I am having a great day!

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

      APL, the brain-eater

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

    this is the closest thing to a wizard using runes to do magic. I wish I could wrap my head around this.

  •  4 หลายเดือนก่อน +1

    I'm so happy that you explicitly advocate for the fundamental principle “the shorter the code, the better”. I have never met anyone besides myself with this opinion. Everyone I know would describe these things as ugly hacks or something like that.
    By the way, I'm all for using good, descriptive variable names. I think one should rather count the syntax elements instead of name lengths. It's just that in APL, a character is often a complete syntax element :)
    I always try to write the shortest code in terms of syntax elements in any language, not just array languages. I also generally prefer functional over imperative and point-free style over lambdas, where available.

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

    These are great, man. People insult the "code golf" of one-liners saying it's not readable, but that misses the power of an expression to be... expressive.

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

    I swear, APL is the ultimate code golf language.

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

    My APL version of join was untested for empty, (my syntax being ) but, as my amended test showed, worked in that case. Thanks for the inspiration to improve my codebase.

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

    I like to try to solve these things with only functional/iterator functions. First up, something that I think would make your Python solution slightly shorter, more efficient, and more elegant: change "".join(s) to chain(*s). The circumvents doing a full pass over s to start.
    My full (not necessarily good) solution: not any(map(mod, Counter(chain(*s)).values(), repeat(len(s)))).

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

    That second language is for wizards only

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

    Why did you need to say "right_tack compose tally" instead of just tally when replacing the dfn {tally omega}? Is it to force monadic version of tally? (Sorry, no APL keyboard)

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

    If you work with vectors/matricies then you might find value in APL.

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

    Hey, I know I'm commenting a pretty old video, but I recently stumbled upon your awesome channel and I saw this font you're using in all APL code snippets, I thought at first it was the default font in dyalog, but here you are using it in the ipython (command line even). What's the name of it?
    Besides that, thank you for your work, I love your content!

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

      APL Unicode 385: aplwiki.com/wiki/Fonts

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

      ​@@code_report Oh wow thank you for a response! To be honest, after writing my comment, I checked aplwiki and immediately saw that font :) Let's pretend that my comment was just an excuse to appreciate your videos ;)

    • @nbme-answers
      @nbme-answers ปีที่แล้ว +1

      beautifully dense

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

    In the APL solution could you have used a ~ (not) instead of 0=? Or could you have just done a NOR reduce instead of and reduce and eliminate that need for the 0= or ~ entirely?

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

    Nice problem. Here's my F# solution
    List.countBy id s
    |> List.map snd
    |> List.forall (fun x -> x % s.Length = 0)

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

      Alternate version
      List.countBy id s
      |> List.map snd
      |> List.forall ((%) >> (|>) s.Length >> (=) 0)

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

    I was thinking, wouldn't it be simpler to just sum the remainder array and check if the sum is 0, instead of turning it into a boolean array?

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

    Had fun watching this video. Here is my Python solution to the problem which I think is quite elegant and quick
    def splittable(word_list:List[str]) -> bool:
    return sum(map(len, word_list)) % len(word_list) == 0

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

    Learned what a fork is. Super chill. Not fantastic on the syntax yet - I can’t parse APL into a syntax tree yet.
    I have an image in my head for the data flowing but I don’t understand how the APL interpreter goes from symbols to that. What the heck gets evaluated first is a question I still struggle to answer. The composes make sense. But are there 3 args to the fork? Oy. I wish there was a colored or extra-dimensional representation of the syntax so I could see what’s doing what to what. Ya know?
    Compose totally clicked though. I’m getting more comfortable with it. I’m real happy with my understanding of airity. Making progress 8)

    • @-..-_-..-
      @-..-_-..- 2 ปีที่แล้ว +2

      five months ago now so im sure youre not still stuck here, but for anyone else: there are only at most 2 arguments to a fork. anything that looks like x (f g h) y where f, g, and h are all two-argument (dyadic) functions will be treated like (x f y) g (x h y). anything that looks like (f g h) x where f and h are one-argument (monadic) functions and g is a dyadic function will be treated as (f x) g (h x).
      to try to mentally parse a syntax tree like you said, start at the right-hand side -- the first function from the right is applied to everything to its right and, if dyadic, only the first thing to its left. then you repeat this process running left.

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

      @@-..-_-..- Yup! I figured it out :) It was ~This video~ that explained it all to me: th-cam.com/video/7-93GzDqC08/w-d-xo.html . I'd like to add it here for posterity, as well.
      Your comment contains all the same information, which is great. Anyone here can use your comment, or this video if the comment still doesn't make it click, yet.

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

    Is that the language used in the Voynich Manuscript? Lol. The learning curve must be very long. I have been programming in many languages but this one is the most original. Good God, What is your IQ?

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

    I do believe you could use NAND instead of AND if you really wanted to shave off another couple chars

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

    whoever made that language definitely would fail a readability test.

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

      For an alien, it’s definitely comprehensible

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

      It's actually shockingly readable when you learn the symbols

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

      it just takes a few hours.

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

      Half the symbols are already known to you if you have studied first year mathematics. Then you just need to remember to start at the right side.

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

      @@glaurung78 yeah, 2 years later i change my mind. readability is a function of familiarity. the more time u spend familiarizing yourself with anything (whatever it is - even if it was gibberish) the more readable it becomes to u. the real question here is : Does it worth to put all of that effort and switch the way your brain thinks about problems ???
      i want real benefits. e.g. vim is not easy and it takes time to get use to it. but it really makes the layer between your fingers and your code so invisible (in this case it worth it). And that is measurable.
      what benefits does APL give me. And those benefits should be so good to make me put all of that effort into it.

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

    I'm super new to APL. How do I enable these rectangular output brackets around arrays in RIDE?

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

      ]boxing on

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

      @@code_report Thanks!

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

    Apl Is a functional language?

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

      It is more functional than most languages in my opinion. Here is a great article on it: mlochbaum.github.io/BQN/doc/functional.html

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

    You will have to admit this is an ancient alien language someday.

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

    I just watched a problem solved with enchanting table language 🤯

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

    A simple and point-free solution in Haskell:
    solve = (==) 1 . length . nub . map length . group . sort . concat

    • @0LoneTech
      @0LoneTech 7 หลายเดือนก่อน

      Counterexample: ["aaaa", "bb"]
      \ss -> all ((0==) . (`mod` length ss) . length) . group . sort . concat $ ss

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

    Please keep doing Haskell versions of your solutions. :)

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

      -length . nub . map (read::String->Int) . wordsBy (not.isDigit)-
      EDIT: doh! I put a solution to another video you did LOL.
      (all . ((0 ==) .) . flip mod . length) ((snd ) . (toList . fromListWith (+)) . ((,1) ) . join)
      Needs:
      {-# LANGUAGE TupleSections #-}
      import Data.Map
      That language extension allows you to use (,) as a unary function with one of the values already in place.
      You could use a list comprehension to avoid the language extension
      [(c, 1) | c

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

    4:10 the closing parenthesis for 'all(' is a ways off, I hope your IDE helps.

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

    I start sweating when someone has a list comprehension, where just passing a generator would do the job.

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

    Best codes are which can be understandable by any one at 3.00 am for resolving a prod issue.
    Not the shortest one.

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

      APL is incredibly good at being comprehensible once you know the syntax. If we want to throw stones at languages for not being very understandable, Id suggest starting at C++, specifically in a 20+ yearold codebase

    • @julians.2597
      @julians.2597 4 หลายเดือนก่อน

      ​@@JamesAudioslavespecifically a codebase that has been extended since that time, not just maintained. Having sections that are clearly written in different versions and with vastly different subsets of the language. Actual horror

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

    nice

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

    Serious question: what is the point of APL beyond 1. obfuscation or 2. elitist flexing or 3. code golf for experts?

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

      Number one it’s pretty fast and number two you can do so much in so little time if you understand the syntax even I don’t yet

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

      it is very expressive. And even if you aren't going to use it, learning how to approach problems with an APL mindset adds more tools to your mental toolbox

    • @-..-_-..-
      @-..-_-..- 2 ปีที่แล้ว

      array languages are very well-optimized for operating on large n-dimensional arrays, even a newcomer like BQN is already faster than languages we would consider fast like C in these cases. i was skeptical at first too but frankly the single unicode symbols for all the primitives actually makes things far more readable when you scale the initial learning cliff of learning them, imo it's because they condense lines of code so you can get all of the pertinent info of a function in the same eyeball-full of reading. in other languages you often have to move your eye and brain all around a screenful of text (or between screenfuls) to fully conceive of a complete unit of a program, and this requires you to process and remember and hold info in your brain that you shouldn't have to because it's already in the source code, it's just that the useful parts of that source code aren't placed on the page in a way that captures their relationships effectively. there are other arguments for it but this adjacency thing was something that stuck out to me that i haven't seen other people bring up.

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

      @@-..-_-..- that's interesting, thanks for letting us know!

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

    Can anyone tell me is it possible to make a career out of APL
    Please

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

      Check out these companies that use APL: github.com/interregna/arraylanguage-companies

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

      @@code_report thank you. I got to knw that chances are less

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

    In Julia:
    using StatsBase: countmap
    s = "abc", "aabc", "bcccc"
    # Julian solution
    sol(x) = iszero((values ∘ countmap ∘ prod)(x) .% length(x))
    sol(s)
    # Almost pointfree but unable due to parsing being different
    fork(unary1, binary, unary2) = x -> binary(unary1(x), unary2(x))
    iszero(fork((values ∘ countmap ∘ prod), .%, length)(s))

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

    m
    i
    n
    d
    b
    l
    o
    w
    n

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

    So falling in love w lisps is making me fall into the array language black hole? Yes! Lmao.

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

    When watching a (higher level) coding video I can sort of follow along, but with APL? Nah, dawg.

  • @defoer3049
    @defoer3049 8 หลายเดือนก่อน

    ta

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

    Honestly I do not vibe with the APL syntax. I do intend to try the language sometime, and I think it has some interesting ideas (at least worth considering, if not implementing). But I dislike its being right-to-left with functions behaving differently from values (which is what makes those implicit forks possible), and it being obfuscated. I do like that it uses Unicode, but IMO keywords are still very useful.
    I'm a C/C++ boi who likes the mathematics of Haskell -- I like fast and rigorous. To me, APL is neither of those, nor is it especially readable (though that is to be expected, as I haven't learned anything like it before).

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

      I totally agree. I have no idea how to google all that Unicode sh*t

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

      @@valera924 you don't google them, you hover with your mouse over them in the toolbar and you get their definition and examples for using them

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

    APL code looks like a password

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

    Basically APL throws readability through the window...
    This would be the language if the programming was introduced in stoneage...

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

    APL is not practical in daily uses. Is not about the language being easy or hard.

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

    No one cares that you can "save a single character", what's important is that the solution is understandable to humans in 30 minutes time. OK?
    No one except 3 programmers dispersed in basements around the globe think that this kind of counterproductive obfuscation is "cool!"

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

    if you want a difficult to read python solution... just
    def canRedist(w);td={};return all(x%len(words)==0 for x in(td,[td.__setitem__(i,td.get(i,0)+1) for i in ''.join(words)])[0].values())