fork(1) download
  1. # Experimenting with callbacks. (4.01)
  2.  
  3. namespace eval lambda {
  4. variable id 0
  5. }
  6.  
  7. proc lambda {capturedList args body} {
  8. set _ _lambda[incr ::lambda::id]
  9. proc $_ $args \
  10. [concat [list foreach {name value} $capturedList {
  11. set $name $value }] \; $body]
  12. return $_
  13. }
  14.  
  15. interp alias {} λ {} lambda
  16.  
  17. proc not1 {unaryPredicate} {
  18. λ [list predicate $unaryPredicate] {item} {
  19. expr ! [$predicate $item]
  20. }
  21. }
  22.  
  23. proc any {seq predicate} {
  24. foreach item $seq {
  25. if {[$predicate $item]} {
  26. return 1
  27. }
  28. }
  29. return 0
  30. }
  31.  
  32. proc all {seq predicate} {
  33. expr ! [any $seq [not1 $predicate]]
  34. }
  35.  
  36. # Main.
  37.  
  38. proc toBool {i} {
  39. if {$i == 0} { return false }
  40. return true;
  41. }
  42.  
  43. proc stringAnyIs {class s} {
  44. toBool [any [split $s {}] [λ [list class $class] {c} {
  45. string is $class $c
  46. }]]
  47. }
  48.  
  49. proc stringAllIs {class s} {
  50. toBool [all [split $s {}] [λ [list class $class] {c} {
  51. string is $class $c
  52. }]]
  53. }
  54.  
  55. while {[gets stdin line] >= 0} {
  56. foreach class {space graph punct} {
  57. puts "\"$line\" any $class: [stringAnyIs $class $line]"
  58. puts "\"$line\" all $class: [stringAllIs $class $line]"
  59. }
  60. }
Success #stdin #stdout 0.01s 5280KB
stdin
     
abcde
!@#%&
ab de
!@ %&
stdout
"     " any space: true
"     " all space: true
"     " any graph: false
"     " all graph: false
"     " any punct: false
"     " all punct: false
"abcde" any space: false
"abcde" all space: false
"abcde" any graph: true
"abcde" all graph: true
"abcde" any punct: false
"abcde" all punct: false
"!@#%&" any space: false
"!@#%&" all space: false
"!@#%&" any graph: true
"!@#%&" all graph: true
"!@#%&" any punct: true
"!@#%&" all punct: true
"ab de" any space: true
"ab de" all space: false
"ab de" any graph: true
"ab de" all graph: false
"ab de" any punct: false
"ab de" all punct: false
"!@ %&" any space: true
"!@ %&" all space: false
"!@ %&" any graph: true
"!@ %&" all graph: false
"!@ %&" any punct: true
"!@ %&" all punct: false