GithubHelp home page GithubHelp logo

Comments (5)

evancz avatar evancz commented on May 17, 2024

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.

jhickner avatar jhickner commented on May 17, 2024

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.

evancz avatar evancz commented on May 17, 2024

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.

jhickner avatar jhickner commented on May 17, 2024

Thanks! Wow, I hadn't messed much with the flow stuff yet! So coo!

from compiler.

evancz avatar evancz commented on May 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.