Menu

Memoize?

Dennis
2014-09-02
2015-05-30
  • Dennis

    Dennis - 2014-09-02

    I have no idea what j does. Could you please explain?

     
    • aditsu

      aditsu - 2015-01-31

      I'm so sorry, I did not receive a notification about this post, I'm just seeing it now for the first time.

      The basic idea for j is to have a recursive function that caches its values.
      The simplest syntax is:
      x a f j
      where f is the function block, x is the parameter for which you want to calculate f(x), and a is a value or an array of values to start with, i.e. f(0), f(1), ... (it can also be an empty array). The "return values" are cached for all invocations, and when using j inside the block, it calls itself recursively, using the cache if available.

      Memoized factorial: 5 1{_(j*}j
      1 is 0!, and the block does n*(n-1)!

      Memoized "n-th fibonacci number": 10 2,{(_j\(j+}j

      In the latest release (0.6.3) I improved j to use a map so it can cache values sparsely (in older versions it used a contiguous array), it can work with multiple parameters (by adding the number of parameters right before j, as in x1 x2 ... xn a f n j) and also with any types, not just positive integers.

      Here's the Levenshtein distance implemented directly on strings:

      "ginger""ninja" L{1$1$={;;0}{_{\_{[[[\]_:)2/z~:a]:~~=!2$2$j+\4$j)@3$j)]2>$0=}{;,}?}{;,}?}?}2j

      (haven't spent too much time on golfing it or making it nice)

       
      • Dennis

        Dennis - 2015-05-30

        I didn't receive a notification for your reply either.

        I get it now. Thank you.

         
  • Dennis

    Dennis - 2014-09-29

    Bump. :P

     

Log in to post a comment.