factor-lang

How do I write to a duplex stream?

有些话、适合烂在心里 提交于 2019-12-24 13:38:21
问题 Suppose I have a Super Simple Socket Server that accepts connections on a port at localhost : : server-new ( port -- stream ) f swap <inet4> utf8 <server> accept drop ; And I can use it like: IN: scratchpad 1204 server-new Which waits for a connection. In a terminal: $ curl localhost:1204 _ Which then waits for the server to reply. server-new leaves a duplex-stream on the stack. T{ duplex-stream { in T{ decoder { stream T{ input-port { handle T{ fd { fd 36 } } } { buffer T{ buffer { size

Unpack an array to the stack at runtime

本秂侑毒 提交于 2019-12-11 10:46:29
问题 I have an array: { 1 2 3 4 } I want to push its contents to the stack. I tried: (sc) { 1 2 3 4 } dup length firstn 1 2 3 4 Great! Inside a word, though: : explode ( a -- * ) dup length firstn ; inline Throws an error Cannot apply “firstn” to a run-time computed value , because firstn calls call and Words which call an input parameter must be declared inline so that a caller which passes in a literal quotation can have a static stack effect. ... and because of call 's semantics it's hard to

Shadowing a vocabulary name from the standard library

。_饼干妹妹 提交于 2019-12-11 04:41:32
问题 For reasons that don't really matter, I need to have a vocabulary named hello-world and I need to be able to run its tests, and I need it to shadow the hello-world from Factor's standard library. I have not been able to do this successfully (I tried reversing vocab-roots but it did not make a difference). Is it possible? 来源: https://stackoverflow.com/questions/37623559/shadowing-a-vocabulary-name-from-the-standard-library

How can I get a connected client's IP address?

岁酱吖の 提交于 2019-12-10 23:28:58
问题 If I have a Super Simple Threaded TCP Server like: USING: accessors io io.encodings.utf8 io.servers io.sockets kernel prettyprint threads ; : handle-client ( -- ) remote-address . ; : <my-server> ( -- threaded-server ) utf8 <threaded-server> "server" >>name 1234 >>insecure [ handle-client ] >>handler ; : start-my-server ( -- ) <my-server> [ start-server ] in-thread start-server drop ; This will just print the text remote-address to the client, which is very helpful. That's because remote

MAIN not executed by Factor on command line

时光总嘲笑我的痴心妄想 提交于 2019-12-10 11:44:47
问题 I'm not seeing any output from my Hello World program. $ cat hello.factor USE: io IN: hello : hello ( -- ) "Hello World!" print ; MAIN: hello $ factor hello.factor $ (No output) $ factor -run=hello Vocabulary does not exist name "hello" $ factor -run=hello hello.factor $ (No output) 回答1: MAIN: defines an entry point for a vocabulary when the vocabulary is passed to run , not necessarily when it is "loaded" from the command line, as you're doing above. The easiest way to make this work is to

Cleave a run-time computed value?

元气小坏坏 提交于 2019-12-07 11:19:27
问题 Cleave is a really useful combinator for minimising code duplication. Suppose I want to classify Abundant, Perfect, Deficient numbers: USING: arrays assocs combinators formatting io kernel math math.order math.primes.factors math.ranges sequences ; IN: adp CONSTANT: ADP { "deficient" "perfect" "abundant" } : proper-divisors ( n -- seq ) dup zero? [ drop { } ] [ divisors dup length 1 - head ] if ; : adp-classify ( n -- a/d/p ) dup proper-divisors sum <=> { +lt+ +eq+ +gt+ } ADP zip H{ } assoc

MAIN not executed by Factor on command line

让人想犯罪 __ 提交于 2019-12-06 16:25:05
I'm not seeing any output from my Hello World program. $ cat hello.factor USE: io IN: hello : hello ( -- ) "Hello World!" print ; MAIN: hello $ factor hello.factor $ (No output) $ factor -run=hello Vocabulary does not exist name "hello" $ factor -run=hello hello.factor $ (No output) MAIN: defines an entry point for a vocabulary when the vocabulary is passed to run , not necessarily when it is "loaded" from the command line, as you're doing above. The easiest way to make this work is to simply issue "hello" run from the UI listener. To actually call the hello word as a script, simply place a

Cleave a run-time computed value?

允我心安 提交于 2019-12-05 16:56:05
Cleave is a really useful combinator for minimising code duplication. Suppose I want to classify Abundant, Perfect, Deficient numbers : USING: arrays assocs combinators formatting io kernel math math.order math.primes.factors math.ranges sequences ; IN: adp CONSTANT: ADP { "deficient" "perfect" "abundant" } : proper-divisors ( n -- seq ) dup zero? [ drop { } ] [ divisors dup length 1 - head ] if ; : adp-classify ( n -- a/d/p ) dup proper-divisors sum <=> { +lt+ +eq+ +gt+ } ADP zip H{ } assoc-clone-like at ; : range>adp-classes ( n -- seq ) 1 swap 1 <range> [ adp-classify ] map ADP dup [ [ [ =

Why use a stack-oriented language? [closed]

北城以北 提交于 2019-11-29 22:42:55
I recently took a look at Factor , and the idea of having a language based around the concept of a stack is very interesting. (This was my first encounter with a stack-oriented language.) However, I don't see any practical advantages of such a paradigm. To me, it just seems like more trouble than it's worth. Why would I use a stack-oriented language such as Factor or Forth? I'm ignoring factors (excuse the pun) such as the availability of tools and libraries. I'm asking only about the language paradigm itself. Stack orientation is an implementation detail. For example, Joy can be implemented

Why use a stack-oriented language? [closed]

孤人 提交于 2019-11-28 18:29:37
问题 I recently took a look at Factor, and the idea of having a language based around the concept of a stack is very interesting. (This was my first encounter with a stack-oriented language.) However, I don't see any practical advantages of such a paradigm. To me, it just seems like more trouble than it's worth. Why would I use a stack-oriented language such as Factor or Forth? I'm ignoring factors (excuse the pun) such as the availability of tools and libraries. I'm asking only about the language