Arc Forumnew | comments | leaders | submitlogin
3 points by fallintothis 4012 days ago | link | parent

Most of my if blocks _are_ a single s-expression for each branch.

I was going to say...

  (def if-with-do (expr)
    (and (caris expr 'if)
         (some [caris _ 'do] expr)))

  (def accum-expr (accumf testf exprs)
    (map [ontree (andf testf accumf) _] exprs))

  (def keep-expr (testf exprs)
    (accum a (accum-expr a testf exprs)))

  (def count-expr (testf exprs)
    (summing s (accum-expr s testf exprs)))

  (defs count-ifs-with-do (exprs) (count-expr if-with-do exprs)
        count-ifs         (exprs) (count-expr [caris _ 'if] exprs)
        ifs-with-do       (exprs) (keep-expr if-with-do exprs)
        ifs               (exprs) (keep-expr [caris _ 'if] exprs))

  arc> (each file '("arc.arc"
                    "strings.arc"
                    "pprint.arc"
                    "code.arc"
                    "html.arc"
                    "srv.arc"
                    "app.arc"
                    "prompt.arc")
         (prn "=== " file)
         (let exprs (readfile file)
           (prn (count-ifs-with-do exprs) "/" (count-ifs exprs))))
  === arc.arc
  11/120
  === strings.arc
  3/18
  === pprint.arc
  5/5
  === code.arc
  0/0
  === html.arc
  4/24
  === srv.arc
  4/21
  === app.arc
  9/53
  === prompt.arc
  2/6
I also find it very interesting that the author doesn't indent the code at all.

I have a feeling (hope) that that was unintentional---like the blog formatting is just eating whitespace or something. Also, the first "Arc if" has a missing right parenthesis and uses = for comparison. :)



3 points by zck 4012 days ago | link

Nice code. Some of my lying-around arc files:

unit-test.arc 2/12

All of my project euler solutions: 4/98

-----

2 points by fallintothis 4012 days ago | link

I was hoping other people would investigate their code! :) My personal arc directory filtered by hand for completeness (I have a lot of half-finished projects lying around), plus some stock Arc stuff I forgot in the last post:

  (each file (lines:tostring:system "find . -name \\*.arc")
    (prn "=== " file)
    (let exprs (errsafe:readfile file)
      (prn (count-ifs-with-do exprs) "/" (count-ifs exprs))))

  === ./transcribe/transcribe.arc
  1/1
  === ./qq/qq-test.arc
  1/3
  === ./qq/qq.arc
  0/10
  === ./macdebug/macdebug.arc
  7/17
  === ./bench/nbody.arc
  0/0
  === ./bench/pidigits.arc
  1/1
  === ./ansi.arc
  1/1
  === ./news.arc
  16/99
  === ./sscontract.arc
  0/0
  === ./blog.arc
  0/0
  === ./libs.arc
  0/0
  === ./profiler/profiler.arc
  0/2
  === ./vim/gen.arc
  0/11
  === ./hygienic.arc
  0/3
  === ./trace/trace.arc
  1/4
  === ./contract/test.arc
  0/1
  === ./contract/contract.arc
  0/9
  === ./contract/special-cases.arc
  0/2
  === ./contract/reconstruct.arc
  0/2
  === ./contract/defcase.arc
  0/1
  === ./contract/test/canonical.arc
  0/4
  === ./contract/test/ssyntax.arc
  0/4
  === ./contract/ssyntax.arc
  0/5
  === ./contract/util.arc
  0/2
  === ./quick-check/quick-check.arc
  1/8
  === ./arity/arity.arc
  0/8

-----

1 point by akkartik 4012 days ago | link

Nice trick, passing accum functions around!

-----