3 ms·
> Its powerful array manipulation primitives can easily be brought forward into a language with modern syntax, and haven't they? Kind of. The languages that on
by mlochbaum 4y ago
> Its powerful array manipulation primitives can easily be brought forward into a language with modern syntax, and haven't they?
Kind of. The languages that one might expect to do this (Julia, R, NumPy) have picked out a few things from APL in an inconsistent way, leaving a lot as well. For example most are missing the generalization of prefix sum that APL calls "scan". So in [0], Conor was able to translate some code to every language in the APL family but not yet any outside of it. Another one, I don't think I've ever seen Replicate[1] outside the APL family. It's a generalization of filter to take an arbitrary count instead of 0 or 1 that's often useful if you know about it.
[0] https://github.com/codereport/array-language-comparisons/blob/main/comparisons/leetcode/P1614_Max_Paren_Depth.md https://github.com/codereport/array-language-comparisons/blo...
[1] https://mlochbaum.github.io/BQN/doc/replicate.html https://mlochbaum.github.io/BQN/doc/replicate.html
- ohbtvz 4y agoI can't make head or tails of the first links, so I'll leave experts to it. The second one, yeah, it's nice syntaxical sugar, but it can be replicated very easily (map + "constant list" + concatenate). And versions of it exist in many languages - even C# has SelectMany https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.selectmany?view=net-7.0 https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...
- mlochbaum 4y agoAll the good APL functions can be implemented easily (but please, they're just built-in functions, not syntactic sugar!). It's the selection of which functions to implement that makes it so much better for working with arrays. "even C#" seems to imply you have less obscure examples than a function in a Linq package, is this the case? I admit I've never looked into Linq but I do see it come up in array language discussions sometimes. It's possible it has a more APL-like philosophy for a slightly different use case. The other difference is that APL uses a function on two arrays, which is nice because you can easily filter based on, say, the previous element. EDIT: No, the C# function seems to only do the mapping and concatenation and doesn't even take a number of copies, that's really not the same.
- occamrazor 4y agoNumpy has replicate (numpy.repeat). Excel has SCAN.
- henrydark 4y agoReplicate looks like numpy's repeat. For example np.repeat(list("abcd"), [2,1,0,2]) is equivalent to the first example in the link.
- mlochbaum 4y agoAh, thank you! I'd tried to find this for implementing BQN in NumPy (never finished) and didn't. Maybe I thought it only worked with a scalar for the number of repetitions.
- eigenspace 4y agoJulia spells "scan" as "accumulate". I don't think we have something exactly like replicate out of the box, but it's a one-line function definition away: replicate(inds, v) = mapreduce(((i, n),) -> fill(v[i], n), vcat, enumerate(inds)) I agree with the spirit of your point though. Having operations like this as part of your 'alphabet' can be a really powerful thing. That's why I think APL is really well suited to being a domain specific language embedded in a language like julia. Shashi took a stab at this a few years ago, but it'd be nice to revive it: https://github.com/shashi/APL.jl https://github.com/shashi/APL.jl
- vharuck 4y agoR has `rep` for replicate: rep(c("a", "b", "c", "d"), c(2, 1, 0, 2)) c("a", "a", "b", "d", "d")