read data from the code


(Bee) #1

Hi all,

Does anyone here remember gwbasic? There is one feature of gwbasic that I
miss from modern programming language. It's the pair of READ function and
DATA statement. An example here:
http://www.antonis.de/qbebooks/gwbasman/data.html

READ function is to read input from DATA statement. This is very helpful
when you need to test your code with specified set of data. Instead of
enter them via keyboard, you simply include the data along with the source
code without changing the code much. In gwbasic, you simply changes the
READ into INPUT function to read input from internal data to from user's
input by keyboard.

Of course, I could make a function to do something like that. But I need it
for teaching swift to complete beginners. I need the feature to be in
language level (as provided by gwbasic).

Is it available? Or is there any similar feature?

Thank you.

Regards,

···

--
-Bee-


(David Turnbull) #2

I couldn't figure out a way to bring back line numbers but here's READ and
DATA...

// Define your data

let DATA = [5,7,1,6,7]

// Change the Int below to whatever type of DATA you have

let READ = {() -> () -> Int

    in var g=DATA.generate();return {()in return g.next()!}}()

// And now you read

print( READ() )

print( READ() + READ() )

print( READ() * READ() )

It's like magic and stuff.

-david

···

On Fri, Dec 11, 2015 at 10:46 PM, Bee via swift-users <swift-users@swift.org > wrote:

Hi all,

Does anyone here remember gwbasic? There is one feature of gwbasic that I
miss from modern programming language. It's the pair of READ function and
DATA statement. An example here:
http://www.antonis.de/qbebooks/gwbasman/data.html


(Bee) #3

Well, it's not really what I want as it still has a custom function (READ).
I think it's not possible in Swift. It's alright, let's move on. Thank you,
anyway. :slight_smile:

···

On Sat, Dec 12, 2015 at 3:37 PM, David Turnbull via swift-users < swift-users@swift.org> wrote:

I couldn't figure out a way to bring back line numbers but here's READ and
DATA...

// Define your data

let DATA = [5,7,1,6,7]

// Change the Int below to whatever type of DATA you have

let READ = {() -> () -> Int

    in var g=DATA.generate();return {()in return g.next()!}}()

// And now you read

print( READ() )

print( READ() + READ() )

print( READ() * READ() )

It's like magic and stuff.

-david

On Fri, Dec 11, 2015 at 10:46 PM, Bee via swift-users < > swift-users@swift.org> wrote:

Hi all,

Does anyone here remember gwbasic? There is one feature of gwbasic that I
miss from modern programming language. It's the pair of READ function and
DATA statement. An example here:
http://www.antonis.de/qbebooks/gwbasman/data.html

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
-Bee-