Comments (5)
Can you show me the JSON object you are working with? findString
returns an empty string (i.e. empty list) when the key is not found. Same with findArray
which returns an empty list when the key is not found.
from compiler.
The json I'm testing with is the result returned from http://www.reddit.com/r/cinemagraphs/hot.json
. Which I've downloaded and named downloaded.json
.
Here's my test code:
import HTTP
import JSON
redditURL = "downloaded.json"
extract res =
case res of
{ Success str -> JSON.fromString str
; _ -> empty }
getUrls res =
let { json = extract res
; children = findArray "children" . findObject "data" $ json
; urls = map (findString "url" . findObject "data") children
}
in urls
main = lift (plainText . show) . lift getUrls . send . constant $ get redditURL
I'm just using plainText
to print the result. It prints as:
[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
The format of the json is like this (with a bunch elided):
{
"data": {
"children: [
{"data": {"url": some url}},
{"data": {"url": some url}},
{"data": {"url": some url}},
{"data": {"url": some url}},
]
}
}
from compiler.
Ok, this is actually a bug in the type checker. The program you sent should have thrown a type error at compile time, so I'll look into what is going on.
The issue was that findObject :: String -> JsonObject JsonValue -> JsonObject
was given a JsonValue
as its second argument. JsonValue
has a constructor JsonObject (JsonObject JsonValue)
, meaning that you have to extract the actual object. The first JsonObject
is a type constructor and the second is a type.
This level of indirection is needed so that JsonNull
, (JsonNumber 4)
and JsonObject empty
can all have the same type without all being valid arguments to things like findObject
and findArray
.
This should work:
import HTTP
import JSON
redditURL = "downloaded.json"
extract res =
case res of
{ Success str -> JSON.fromString str
; _ -> empty }
valueToObj v = case v of { JsonObject obj -> obj ; _ -> empty }
getUrls res =
let { json = extract res
; children = findArray "children" . findObject "data" $ json
; urls = map (findString "url" . findObject "data" . valueToObj) children
}
in urls
main = lift (flow down . map asText) . lift getUrls . sendGet $ constant redditURL
By the way, this is totally awesome! I had it output everything as images and it was really cool :D
You might be interested in something like this:
groups n lst =
case lst of
{ [] -> [] ; x:xs -> take n lst : groups n (drop n lst) }
tile w tiles =
flow down . intersperse (spacer 10 10) . map (flow right) $ groups (w `div` tileSize) tiles
from compiler.
Thanks! Wow, I hadn't messed much with the flow stuff yet! So coo!
from compiler.
No problem!
See the other issue on fittedImage
. If you do that fix, try adding replacing main
with:
main = lift (flow down . map (fittedImage 300 200)) . lift getUrls . sendGet $ constant redditURL
Again, very cool idea! I'd love to see what it ends up looking like!
from compiler.
Related Issues (20)
- Identity type alias causes problems HOT 2
- elm publish - tried to fetch https://package.elm-lang.org... "But it came back as 413 Request Entity Too Large" HOT 9
- `elm publish` hangs indefinitely at GitHub tag check with large package HOT 4
- Compiler panic when unifying inner skolem with an outer unification variable HOT 4
- Compiler error produces unexpected output when rendered in a terminal HOT 3
- Package opvasger/comparable has been deleted & can no longer be installed HOT 1
- elm-live dies hard when it cant find output files. HOT 8
- What's the state of the Elm repo? HOT 9
- Simplify type system HOT 3
- Clarify type syntax HOT 3
- Docker build fails with rejected dependency HOT 1
- record extended with additional field not accepted as extensible record with record with same fields HOT 1
- Send Bytes through ports HOT 1
- Issue with extensible records and unit type HOT 1
- Permission Denied Error Due to Unset `ELM_HOME` Environment Variable in `Stuff` Module HOT 4
- Error message should be more informative when integer literal is being used as both integer and float HOT 3
- terminal/src/Develop/StaticFiles.hs:91:3: error: Exception when trying to run compile-time code: reactor: changeWorkingDirectory: does not exist HOT 3
- elm publish under zsh not printing error message HOT 1
- Inaccurate information in installation instructions HOT 1
- Let binding cyclic value detection is stronger than top level HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compiler.