Arc Forumnew | comments | leaders | submitlogin
2 points by almkglor 5862 days ago | link | parent

Here's an alternative using Anarki scanners (or if you prefer, you can always use coerce which is cheaper on the memory; however scanners are lazier and, if you need to process less than maybe 1/4 the original string, is cheaper in memory too)

  (def shortify (i s)
    (let shorten nil
      (= shorten
         (fn (i ss)
           (if (is i 0)
               nil
               (when ss
                 (pr:car ss)
                 (if (and (caris ss #\newline) (is (cadr ss) #\newline))
                     (let ss (cdr ss)
                       (while (caris ss #\newline)
                         (prn)
                         (zap cdr ss))
                       (shorten (- i 1) ss))
                     (shorten i (cdr ss)))))))
      ; replace with (coerce s 'cons) to taste
      (tostring:shorten i (scanner-string s))))
Untested of course, and you better make sure the parens match, I don't have access to vi right now ~.~ ^^.

Edit: s/l/ss/, because l (small letter L) looks like 1 (number one)

Edit2: err. Actually I thought i would be number of paras. Oh well. At least now you have more generic code ^^:

  (shortify 3 "foo\n\nbar\n\nnitz\n\nkoo\n\n")
  => "foo\n\nbar\n\nnitz\n\n"
As an aside, if you want you can study also the paragraph scanner in Arki, the wiki in Anarki.


1 point by wfarr 5862 days ago | link

I'll definitely give this one a shot, because I have a tendency to get a bit long-winded. =D

-----