<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 8, 2016 at 1:06 AM, mike thomas <span dir="ltr"><<a href="mailto:mjt464@gmail.com" target="_blank">mjt464@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi all.</div><div><br></div><div>Having returned to commercial software development after a 10 year break in environmental regulation and travel, it was not long before I was using my favorite language Haskell, fortunately, as part of my job this time.</div><div><br></div><div>Haskell has changed in many ways since last I used it, and I am having an internal (lack of) knowledge collision with the problem below:</div><div><br></div><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">As set out in the example below, given a polygon as GeoJSON from a Postgis enabled Postgres datasbase, I want to get to a list of Double tuples: [(Double,Double)], using Aeson, to draw some polygons. I am stopped at the point of dismembering the nested coordinate arrays.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">With Stackage nightly-2016-09-01 ghci, and the series of functions defined at the end of this message, I get a runtime type error regarding Vectors, which baffles me.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">Question 1. Why does Aeson nest the arrays using list syntax around the Array type constructor arguments?</p></div></blockquote><div>This is just the Show instance for Values, you can `encode' to print the actual json representation.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">Question 2. How do I best convert this part of the AST:</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">"(Array [Array [Array [Number 4.5305923601,Number 50.6585120423],Array [Number 4.5307511543,Number 50.657719833],Array [Number 4.5310580333,Number 50.657539732],Array [Number 4.5309023972,Number 50.6584422261],Array [Number 4.5308797482,Number 50.6586166629],Array [Number 4.5305923601,Number 50.6585120423]]])"</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">to a list of Double tuples?</p></div></blockquote><div>I usually try to avoid doing custom parsing, it's much easier to use an existing instance when you can.</div><div><br></div><div>Try this:</div><div><br></div><div><div><font face="monospace, monospace">#!/usr/bin/env stack</font></div><div><font face="monospace, monospace">-- stack --resolver nightly-2016-09-01 --install-ghc runghc --package aeson</font></div><div><font face="monospace, monospace">{-# LANGUAGE DeriveGeneric #-}</font></div><div><font face="monospace, monospace">{-# LANGUAGE OverloadedStrings #-}</font></div><div><font face="monospace, monospace">module Main (main) where</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">import GHC.Generics</font></div><div><font face="monospace, monospace">import Data.Aeson</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">data Geo = Geo { coordinates :: [[(Double, Double)]] } deriving (Generic, Show)</font></div><div><font face="monospace, monospace">instance ToJSON Geo</font></div><div><font face="monospace, monospace">instance FromJSON Geo</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">main :: IO ()</font></div><div><font face="monospace, monospace">main = do</font></div><div><font face="monospace, monospace"> let ex1 = eitherDecode "[[1,2]]" :: Either String Value</font></div><div><font face="monospace, monospace"> print ex1</font></div><div><font face="monospace, monospace"> print $ fmap encode ex1</font></div><div><font face="monospace, monospace"> let ex2 = "{\"type\":\"Polygon\",\"coordinates\":[[[4.5305923601,50.6585120423],[4.5307511543,50.657719833],[4.5310580333,50.657539732],[4.5309023972,50.6584422261],[4.5308797482,50.6586166629],[4.5305923601,50.6585120423]]]}"</font></div><div><font face="monospace, monospace"> print (eitherDecode ex2 :: Either String Geo)</font></div></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">I've tried several variations of the function dropOuterArray to try and resolve this run-time error, with no success.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">All help welcome, and thanks for your time</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">Mike.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">========= GHCI Session With Error ======================</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">p = parseGeoJSONGeometry "{\"type\":\"Polygon\",\"<wbr>coordinates\":[[[4.5305923601,<wbr>50.6585120423],[4.5307511543,<wbr>50.657719833],[4.5310580333,<wbr>50.657539732],[4.5309023972,<wbr>50.6584422261],[4.5308797482,<wbr>50.6586166629],[4.5305923601,<wbr>50.6585120423]]]}"</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal">p (Just (String "Polygon"),Just (Array [Array [Array [Number 4.5305923601,Number 50.6585120423],Array [Number 4.5307511543,Number 50.657719833],Array [Number 4.5310580333,Number 50.657539732],Array [Number 4.5309023972,Number 50.6584422261],Array [Number 4.5308797482,Number 50.6586166629],Array [Number 4.5305923601,Number 50.6585120423]]]))</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal">p1 = dropOuterArrayJ (snd p)</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal">p1 [Array [Array [Number 4.5305923601,Number 50.6585120423],Array [Number 4.5307511543,Number 50.657719833],Array [Number 4.5310580333,Number 50.657539732],Array [Number 4.5309023972,Number 50.6584422261],Array [Number 4.5308797482,Number 50.6586166629],Array [Number 4.5305923601,Number 50.6585120423]]]</p><p style="margin:0px;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal">p2 = dropOuterArray p1</p><div><b></b><i></i><u></u><sub></sub><sup></sup><strike><br></strike></div><div><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">:151:21: error: • Couldn't match type ‘Data.Vector.Vector Value’ with ‘Value’ Expected type: Value Actual type: Array • In the first argument of ‘dropOuterArray’, namely ‘p1’ In the expression: dropOuterArray p1 In an equation for ‘p2’: p2 = dropOuterArray p1</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">============ Code ====================</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">{-# LANGUAGE OverloadedStrings, DeriveGeneric, DeriveAnyClass, ScopedTypeVariables, LambdaCase, FlexibleContexts #-}</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">module GeoJSONGeometry (<br> parseGeoJSONGeometry<br> ,Coordinates<br> ,dropOuterArrayJ<br> ,dropOuterArray<br> ,Pt<br> ,gTypeString<br>) where</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">import Data.Aeson<br>import Data.Aeson.Types --(parse, parseMaybe, parseEither, Value(..))<br>import GHC.Generics<br>import Data.ByteString.Lazy.Char8<br>import Data.HashMap.Strict<br>import qualified Data.Vector as V <br>import Data.Either.Extra (fromRight)<br>import Data.Maybe (fromJust)<br>import Control.Applicative</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">import Codec.Picture<br>import Graphics.Rasterific<br>import Graphics.Rasterific.Texture</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">import GHC.Generics</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)"><br>parseGeoJSONGeometry gjg =<br> let<br> eresult = (eitherDecode (pack gjg)) :: Either String Object -- Value -- Object<br> result = (fromRight eresult)<br> gType = Data.HashMap.Strict.lookup "type" result<br> gCoords = Data.HashMap.Strict.lookup "coordinates" result<br> in<br> (gType, gCoords)</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)">gTypeString (Just (Data.Aeson.Types.String s)) = s<br> <br>dropOuterArrayJ (Just (Array u)) = u</p><p style="margin:0px 0px 1em;padding:0px;border:0px;text-align:left;color:rgb(36,39,41);text-transform:none;line-height:19.5px;text-indent:0px;letter-spacing:normal;clear:both;font-family:arial,"helvetica neue",helvetica,sans-serif;font-size:15px;font-style:normal;font-weight:normal;word-spacing:0px;white-space:normal;background-color:rgb(255,255,255)"><br>dropOuterArray (Array u) = u<br></p></div><div><br></div></div>
<br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div></div>