Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Topic_variable

View file

@ -0,0 +1,9 @@
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable.
A topic variable is a [[Special variables|special variable]] with a very short name which can also often be omitted.
Demonstrate the utilization and behaviour of the topic variable within the language and explain or demonstrate how the topic variable behaves under different levels of nesting or scope, if this applies, within the language.
For instance you can (but you don't have to) show how the topic variable can be used by assigning the number <math>3</math> to it and then computing its square and square root.
<br><br>

View file

@ -0,0 +1,47 @@
on run
1 + 2
ap({square, squareRoot}, {result})
--> {9, 1.732050807569}
end run
-- square :: Num a => a -> a
on square(x)
x * x
end square
-- squareRoot :: Num a, Float b => a -> b
on squareRoot(x)
x ^ 0.5
end squareRoot
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- A list of functions applied to a list of arguments
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
set lst to {}
repeat with f in fs
tell mReturn(contents of f)
repeat with x in xs
set end of lst to |λ|(contents of x)
end repeat
end tell
end repeat
return lst
end ap
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,51 @@
on run
script
-- The given function applied to the value 3
on |λ|(f)
mReturn(f)'s |λ|(3)
end |λ|
end script
-- Here, 'result' is bound to the script above
map(result, {square, squareRoot})
--> {9, 1.732050807569}
end run
-- square :: Num a => a -> a
on square(x)
x * x
end square
-- squareRoot :: Num a, Float b => a -> b
on squareRoot(x)
x ^ 0.5
end squareRoot
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,2 @@
3
Disp *3▶Dec,i

View file

@ -0,0 +1,10 @@
function Sum (x, y)
Sum = x + y # using name of function
end function
function SumR (x, y)
return x + y # using Return keyword which always returns immediately
end function
print Sum (1, 2)
print SumR(2, 3)

View file

@ -0,0 +1,6 @@
user=> 3
3
user=> (Math/pow *1 2)
9.0
user=> (Math/pow *2 0.5)
1.7320508075688772

View file

@ -0,0 +1 @@
: myloop 11 1 do i . loop cr ; myloop

View file

@ -0,0 +1,5 @@
: ^2 dup * ;
: sqrt 0 tuck ?do 1+ dup 2* 1+ +loop ;
: topic >r r@ ^2 . r@ sqrt . r> drop ;
23 topic

View file

@ -0,0 +1,20 @@
' FB 1.05.0 Win64
' Three different ways of returning a value from a function
Function Sum (x As Integer, y As Integer) As Integer
Sum = x + y '' using name of function
End Function
Function Sum2 (x As Integer, y As Integer) As Integer
Function = x + y '' using Function keyword
End Function
Function Sum3 (x As Integer, y As Integer) As Integer
Return x + y '' using Return keyword which always returns immediately
End Function
Print Sum (1, 2)
Print Sum2(2, 3)
Print Sum3(3, 4)
Sleep

View file

@ -0,0 +1,33 @@
package main
import (
"math"
"os"
"strconv"
"text/template"
)
func sqr(x string) string {
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return "NA"
}
return strconv.FormatFloat(f*f, 'f', -1, 64)
}
func sqrt(x string) string {
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return "NA"
}
return strconv.FormatFloat(math.Sqrt(f), 'f', -1, 64)
}
func main() {
f := template.FuncMap{"sqr": sqr, "sqrt": sqrt}
t := template.Must(template.New("").Funcs(f).Parse(`. = {{.}}
square: {{sqr .}}
square root: {{sqrt .}}
`))
t.Execute(os.Stdout, "3")
}

View file

@ -0,0 +1,4 @@
Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> map (^2) it
[1,4,9,16,25,36,49,64,81,100]

View file

@ -0,0 +1,3 @@
example=: *:, %: NB. *: is square, %: is square root
example 3
9 1.73205

View file

@ -0,0 +1,3 @@
Example=: verb def '(*: y), (%: y)'
Example 3
9 1.73205

View file

@ -0,0 +1,2 @@
(*:, %:) 3
9 1.73205

View file

@ -0,0 +1,4 @@
julia> 3
3
julia> ans * ans, ans - 1
(9, 2)

View file

@ -0,0 +1,9 @@
// version 1.1.2
fun main(args: Array<String>) {
3.let {
println(it)
println(it * it)
println(Math.sqrt(it.toDouble()))
}
}

View file

@ -0,0 +1,2 @@
for _ in 1..10:
echo "Hello World!"

View file

@ -0,0 +1,4 @@
import sequtils
let x = [1, 2, 3, 4, 5]
let y = x.mapIt(it * it)
echo y # @[1, 4, 9, 16, 25]

View file

@ -0,0 +1 @@
3 dup sq swap sqrt

View file

@ -0,0 +1,2 @@
3
[sqrt(%),%^2]

View file

@ -0,0 +1 @@
print sqrt . " " for (4, 16, 64)

View file

@ -0,0 +1,8 @@
for (1..2) {
print "outer $_:\n";
local $_;
for (1..3) {
print " inner $_,";
}
print " fini\n";
}

View file

@ -0,0 +1,7 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">_</span>
<span style="color: #000000;">_</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">_</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">_</span><span style="color: #0000FF;">*</span><span style="color: #000000;">_</span>
<!--

View file

@ -0,0 +1,9 @@
PicoLisp sets the value of the variable (symbol) '@' to the result of
conditional and controlling expressions in flow- and logic-functions (cond, if,
and, when, while, etc.).
Within a function or method '@' behaves like a local variable, i.e. its value is
automatically saved upon function entry and restored at exit.
For example, to read the current input channel until EOF, and print the square
of every item which is a number:

View file

@ -0,0 +1,9 @@
(while (read)
(when (num? @)
(println (* @ @)) ) )
abc # Not a number
7 # Number
49 # -> print square
xyz # Not a number
3 # Number
9 # -> print square

View file

@ -0,0 +1,2 @@
65..67 | ForEach-Object {$_ * 2} # Multiply the numbers by 2
65..67 | ForEach-Object {[char]$_ } # ASCII values of the numbers

View file

@ -0,0 +1 @@
65..67 | Where-Object {$_ % 2}

View file

@ -0,0 +1 @@
65..70 | Format-Wide {$_} -Column 3 -Force

View file

@ -0,0 +1,5 @@
>>> 3
3
>>> _*_, _**0.5
(9, 1.7320508075688772)
>>>

View file

@ -0,0 +1,7 @@
/*REXX program shows something close to a "topic variable" (for functions/subroutines).*/
parse arg N /*obtain a variable from the cmd line. */
call squareIt N /*invoke a function to square da number*/
say result ' ' /*display returned value from the func.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
squareIt: return arg(1) ** 2 /*return the square of passed argument.*/

View file

@ -0,0 +1,57 @@
#lang racket
(module topic1 racket
;; define $ as a "parameter", but make it look like a plain identifier
(provide $ (rename-out [$if if] [$#%app #%app]))
(define current-value (make-parameter #f))
(define-syntax $
(syntax-id-rules (set!)
[(_ x ...) ((current-value) x ...)]
[(set! _ val) (current-value val)]
[_ (current-value)]))
;; make an `if' form that binds it to the condition result
(define-syntax-rule ($if C T E)
(parameterize ([current-value C])
(if $ T E)))
;; function application with []s uses the topic variable for the first arg
(define-syntax ($#%app stx)
(syntax-case stx ()
[(_ f x y ...) (equal? #\[ (syntax-property stx 'paren-shape))
#'(parameterize ([current-value x]) (f y ...))]
[(_ f x ...) #'(f x ...)])))
(module topic2 racket
;; better variant: define `$' as a syntax parameter, which is adjusted to an
;; actual local binding; make it work in `if', and have a function definition
;; form that binds it to the actual arguments
(provide $ (rename-out [$if if]) defun)
(require racket/stxparam)
(define-syntax-parameter $ (λ(stx) (raise-syntax-error '$ "not in scope")))
(define-syntax-rule ($if C T E)
(let ([c C]) (syntax-parameterize ([$ (make-rename-transformer #'c)])
(if c T E))))
(define-syntax-rule (defun name body ...)
(define (name arg)
(syntax-parameterize ([$ (make-rename-transformer #'arg)])
body ...)))
)
(module sample1 racket
(require (submod ".." topic1))
(if (memq 2 '(1 2 3)) (cadr $) 'missing)
;; => 3
(define (foo) (list (sqrt $) (* $ $)))
[foo 9]
;; => '(3 81)
)
(require 'sample1)
(module sample2 racket
(require (submod ".." topic2))
(if (memq 2 '(1 2 3)) (cadr $) 'missing)
;; => 3
(defun foo (list (sqrt $) (* $ $)))
(foo 9)
;; => '(3 81)
)
(require 'sample2)

View file

@ -0,0 +1,5 @@
$_ = 'Outside';
for <3 5 7 10> {
print $_;
.³.map: { say join "\t", '', $_, .², .sqrt, .log(2), OUTER::<$_>, UNIT::<$_> }
}

View file

@ -0,0 +1,9 @@
see "sum1 = " + sum1(1,2) + nl
see "sum2 = " + sum2(2,3) + nl
func sum1 (x, y)
sum = x + y
return sum
func sum2 (x, y)
return x + y

View file

@ -0,0 +1,7 @@
while DATA.gets # assigns to $_ (local scope)
print # If no arguments are given, prints $_
end
__END__
This is line one
This is line two
This is line three

View file

@ -0,0 +1,4 @@
DATA.gets
p [$_.to_i ** 2, Math.sqrt($_.to_i)] #=> [9, 1.7320508075688772]
__END__
3

View file

@ -0,0 +1,13 @@
object TopicVar extends App {
class SuperString(val org: String){
def it(): Unit = println(org)
}
new SuperString("FvdB"){it()}
new SuperString("FvdB"){println(org)}
Seq(1).foreach {println}
Seq(2).foreach {println(_)}
Seq(4).foreach { it => println(it)}
Seq(8).foreach { it => println(it + it)}
}

View file

@ -0,0 +1 @@
say [9,16,25].map {.sqrt}; # prints: [3, 4, 5]

View file

@ -0,0 +1,7 @@
- 3.0;
val it = 3.0 : real
- it * it;
val it = 9.0 : real
- Math.sqrt it;
val it = 3.0 : real
-

View file

@ -0,0 +1,2 @@
3 -> \($-1! $+1!\) -> $*$ -> [$-1..$+1] -> '$;
' -> !OUT::write

View file

@ -0,0 +1,2 @@
multiply 3 4 # We assume this user defined function has been previously defined
echo $? # This will output 12, but $? will now be zero indicating a successful echo

View file

@ -0,0 +1,6 @@
var T // global scope
var doSomethingWithT = Fn.new { [T * T, T.sqrt] }
T = 3
System.print(doSomethingWithT.call())

View file

@ -0,0 +1,5 @@
a,_,c:=List(1,2,3,4,5,6) //-->a=1, c=3, here _ is used as "ignore"
3.0 : _.sqrt() : println(_) //-->"1.73205", _ (and :) is used to "explode" a computation
// as syntactic sugar
1.0 + 2 : _.sqrt() : _.pow(4) // no variables used, the compiler "implodes" the computation
// --> 9