Hi!
Smalltalk is written with lowercase t, as one word. ;-)
IIRC you can get `(foo / bar) floor` as `foo // bar`.
Instead of parsing text and doing `stringWithNumberLines lines do:`, why
not just use literal array of numbers like `#(4 5 6 ...) do: ...` (you
can have newlines as separators inside literal array as well, so you can
reuse format of your method, just don't use string but more natural
container)? And since you are computing a total, the correct
Smalltalkish way is to use #inject:into: instead of #do: (how exactly is
left as an exercise for the reader :-) ).
Instead of `Platform newXhr`, try to use `Platform fetch:` family of
URLs, they build on more modern fetch() browser API.
The problem of your loadData is that getting data is asynchronous; the
method actually ends immediately, returning self as the default and
initiates the loading process, but result is only present in the future.
One way is to use callbacks, actually you could write
loadData: aUrl thenDo: aBlock
Platform newXhr
open: 'GET' url: aUrl ;
onload: aBlock;
onerror: [ :error | Transcript show:'Request Failed:', error ];
send
and in your code you should pass the block which uses the data once they
come in. As mentioned, using fetch API is probably better; fetch API
returns a Promise of the result so it could be something like (guessing,
not remembering actual fetch API details):
loadData: aUrl thenDo: aBlock
(Platform fetch: aUrl)
then: aBlock
catch: [ :error | Transcript show:'Request Failed:', error ]
and then call `foo loadData: myUrl thenDo: [ :data | ... ]`, or
loadData: aUrl
^ Platform fetch: aUrl "maybe with options"
and then treat a Promise directly, as in
(foo loadData: myUrl) then: [ :data | ... ]
(nitpick: please use space after keyword: messages, it's how it is
common in Smalltalk, unlike in ObjectiveC which took the syntax and idea
of objects from Smalltalk but where they don't put the space and keyword:)
Have fun,
Herby
On 4. 12. 2019 12:59, Abraham Palmer wrote:
> I have a longer-term project of using Amber as the front-end of secure
> and distributed applications built on HoloChain
> <
https://holochain.org/>. I'm new to SmallTalk though and have been
> enjoying learning in Amber via the coding challenges at Advent of Code
> <
https://adventofcode.com/2019>. There are two challenges each day and
> I think they normally build on eachother so you also get practice
> modifying/extending an existing solution. I keep running into the things
> I don't know how to do, but often just a search in Helios on something
> like "floor" comes up with what I need. I was hoping others might want
> to play along and share problems/solutions. For instance I haven't been
> able to get the load of the data input from a URL working yet
> (loadData). Here's some working code from the Day 1 challenge to give
> you an idea.
>
> |
> TestCasesubclass:#AOCTest
> slots: {}
> package: 'Examples'!
>
> !AOCTestmethodsFor:'tests'!
>
> testFuel
> "Run the tests in the example"
>
> selfassert:(Modulenew:12)fuel =2.
> selfassert:(Modulenew:14)fuel =2.
> selfassert:(Modulenew:1969)fuel =966.
> selfassert:(Modulenew:100756)fuel =50346.
> !!
>
> Objectsubclass:#Day
> slots: {#star1. #star2}
> package: 'Examples'!
>
> !DaymethodsFor:'as yet unclassified'!
> Objectsubclass:#Intcode
> slots: {#memory. #step}
> package: 'Examples'!
>
> Objectsubclass:#Module
> slots: {#mass. #cumfuel}
> package: 'Examples'!
>
> !ModulemethodsFor:'as yet unclassified'!
>
> fuel
> "Fuel is now pre-calculated when Module created."
>
> ^cumfuel
> !
>
> fuelMass:aMass
> "Recursively add in additional mass needed to account for the fuel.
> Fuel required to launch a given module is based on its mass.
> Specifically, to find the fuel required for a module, take its mass,
> divide by three, round down, and subtract 2."
> |thefuel|
>
> thefuel :=(aMass /3)floor -2.
> thefuel >0
> ifTrue:[cumfuel :=cumfuel +thefuel.selffuelMass:thefuel ]
> ifFalse:[^cumfuel ].
> !
>
> mass:aMass
> "Set the mass"
>
> mass :=aMass.
> cumfuel :=0.
> selffuelMass:aMass.
> !!
> |
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "amber-lang" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
amber-lang+...@googlegroups.com
> <mailto:
amber-lang+...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/amber-lang/36d89bf9-0a94-47c5-9495-054bc5069f1a%40googlegroups.com
> <
https://groups.google.com/d/msgid/amber-lang/36d89bf9-0a94-47c5-9495-054bc5069f1a%40googlegroups.com?utm_medium=email&utm_source=footer>.