I have a buch of accounts stored in a string dictionary and i would like to count the number of accounts existing, so basicly a ".count" but to find the number of dictionaries created.
var dictionary: [String : [ String ]] = ["" : []]
let storeString = "StoreString"
func addUpdateArray(strings: [String], index: Int) {
let locator = storeString + index.description
dictionary[locator] = strings
}
addUpdateArray(["Account1", "Hy"], 1)
addUpdateArray(["Account2", "Hey"], 3)
and now I would like to see how many accounts are have created of the kind dictionary, is ther a way?
Something like this?
var accounts = [String:[String:String]]() // or whatever your structure is
accounts["Edmund"] = [
"amount": "23.87",
"curreny": "dollars"
]
accounts["Baldrick"] = [
"amount": "23.87",
"curreny": "dollars"
]
accounts["Percy"] = [
"amount": "87.00",
"curreny": "peso"
]
println(accounts.keys.array.count) // 3
If you have dictionary of dictionaries and you want to count the number of actual values inside, you can do it like this:
var accounts = [
"accountsGroup1" : ["account1", "account2", "account3", "account4"],
"accountsGroup2" : ["account1", "account2"],
"accountsGroup3" : ["account1", "account2", "account3", "account4"]
]
let accountsCount = accounts.values.map { $0.count }
let numberOfAllAccounts = reduce(accountsCount, 0) { $0 + $1 }
println(numberOfAllAccounts)
Related
local config = {
['dog'] = {amount = 500},
['dog'] = {amount = 600},
['dog'] = {amount = 700},
}
for k,v in pairs(config) do
print(v)
end
Output: table:0x244b890 - (500)
How to output all values from the table? Is it possible with the same keys?
I don't think it's possible, because in Lua tables, the keys are unique. So, I am afraid you have to use another way to represent your data.
local config = {
{ 'dog', amount = 500 },
{ 'dog', amount = 600 },
{ 'dog', amount = 700 }
}
for Index = 1, #config do
print(config[Index].amount)
end
The result will be:
500
600
700
My dataset looks like the below:
Make: AUSTIN
Models:
1000
1300
Make: Ferrari
Models:
458
La Ferrari
I will like this in a JSON format, as below:
{
make: "AUSTIN",
models: [
{model: "1000"},
{model: "1300"}
]
},
{
make: "Ferrari",
models: [
{model: "458"},
{model: "La Ferrari"}
]
}
It's a very large dataset so I can't do it manually.
Looked around online and didn't find anything suitable.
Thanks in advance!
As far as I understand your question, I would like to answer it.
You can do something like this.
function getFormatted(s){
const total = []
const lines = s.split('\n');
let index = 0;
while(lines[index]){
const make = lines[index];
const obj = {
make: make.replace('Make: ',''),
models: []
}
// index + 1 will be 'Models:'
let modelCurrentIndex = index + 2;
let currentModel = lines[modelCurrentIndex];
// Check until the next occurrence of 'Make: '
while(currentModel && !currentModel.startsWith("Make:")){
obj.models.push({model: currentModel});
modelCurrentIndex += 1;
currentModel = lines[modelCurrentIndex];
}
index = modelCurrentIndex;
total.push(obj);
}
return JSON.stringify(total);
}
Sample Web Page would be like this
After calling this function,
Explanation:
The First Index of the lines should be identified as 'make' and that index + 2 will be identified as the starting point of Models.
The while loop will add the models to the array in the object until it identifies the line that starts with 'Make:'. After that, the index is moved and the process gets repeated.
Make sure you are entering the values with a line break!
I want to create a binding of the Plotly.js library to Fable.
I am looking at this js code
import React from 'react';
import Plot from 'react-plotly.js';
class App extends React.Component {
render() {
return (
<Plot
data={[
{
x: [1, 2, 3],
y: [2, 6, 3],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
},
{type: 'bar', x: [1, 2, 3], y: [2, 5, 3]},
]}
layout={ {width: 320, height: 240, title: 'A Fancy Plot'} }
/>
);
}
}
and my (faulty) attempt of creating a simple test binding looks like this
open Fable.Core
open Fable.Core.JsInterop
open Browser.Types
open Fable.React
// module Props =
type Chart =
|X of int list
|Y of int List
|Type of string
type IProp =
| Data of obj list
let inline plot (props: IProp) : ReactElement =
ofImport "Plot" "react-plotly.js" props []
let myTrace = createObj [
"x" ==> [1,2,3]
"y" ==> [2,6,3]
"type" ==> "scatter"
"mode" ==> "lines"
]
let myData = Data [myTrace]
let testPlot = plot myData
But obviously it does not work. How do I get it to work? Also, what does {[...]} mean? I am new to Javascript, and as far as I know {...} denotes an object which must contain name value pairs, and [...] denotes an array. So {[...]} seems to denote an object with a single nameless member that is an array, but as far as I know, there are no objects with nameless members.
I have been able to reproduce the example you linked. Please note that I don't Plotly and that I went the empiric way and so things can probably be improved :)
I have created the code as I would probably have done it if I had to use it in my production app. So there is a bit more code than in your question because I don't use createObj.
If you don't like the typed DSL you can always simplify it, remove it and use createObj or anonymous record like I did for the marker property :)
You need to install both react-plotly.js plotly.js in your project.
open Fable.Core.JsInterop
open Fable.Core
open Fable.React
// Define props using DUs this helps create a typed version of the React props
// You can then transform a list of props into an object using `keyValueList`
[<RequireQualifiedAccess>]
type LayoutProps =
| Title of string
| Width of int
| Height of int
// GraphType is marked as a `StringEnum` this means
// the value will be replace at compile time with
// their string representation so:
// `Scatter` becomes `"scatter"`
// You can customise the output by using `[<CompiledName("MyCustomName")>]
[<RequireQualifiedAccess; StringEnum>]
type GraphType =
| Scatter
| Bar
[<RequireQualifiedAccess; StringEnum>]
type GraphMode =
| Lines
| Points
| Markers
| Text
| None
[<RequireQualifiedAccess>]
type DataProps =
| X of obj array
| Y of obj array
| Type of GraphType
| Marker of obj
// This is an helpers to generate the `flagList` waited by Plotly, if you don't like it you can just remove
// member and replace it with `| Mode of string` and so you have to pass the string by yourself
static member Mode (modes : GraphMode seq) : DataProps =
let flags =
modes
|> Seq.map unbox<string> // This is safe to do that because GraphMode is a StringEnum
|> String.concat "+"
unbox ("mode", flags)
[<RequireQualifiedAccess>]
type PlotProps =
| Nothing // Should have real props here is there exist more than Data and Layout
// Here notes that we are asking for an `Array` or Data
// Array being the type expected by the JavaScript library
// `DataProps seq` is our way to represents props
static member Data (dataList : (DataProps seq) array) : PlotProps =
let datas =
dataList
|> Array.map (fun v ->
keyValueList CaseRules.LowerFirst v // Transform the list of props into a JavaScript object
)
unbox ("data", datas)
static member Layout (props : LayoutProps seq) : PlotProps =
unbox ("layout", keyValueList CaseRules.LowerFirst props)
// All the example I saw from react-plotly was using this factory function to transform the plotly library into a React component
// Even, the example you shown if you look at the Babel tab in the live example
let createPlotlyComponent (plotly : obj) = import "default" "react-plotly.js/factory"
// Immport the plotly.js library
let plotlyLib : obj = import "default" "plotly.js"
// Apply the factory on the plotly library
let Plot : obj = createPlotlyComponent plotlyLib
// Helper function to instantiate the react components
// This is really low level, in general we use `ofImport` like you did but if I use `ofImport` then I got a React error
let inline renderPlot (plot : obj) (props : PlotProps list) =
ReactBindings.React.createElement(plot, (keyValueList CaseRules.LowerFirst props), [])
let root =
// Here we can render the plot using our Typed DSL
renderPlot
Plot
[
PlotProps.Data
[|
[
DataProps.X [| 1; 2; 3 |]
DataProps.Y [| 2; 6; 3 |]
DataProps.Type GraphType.Scatter
DataProps.Mode
[
GraphMode.Lines
GraphMode.Points
]
DataProps.Marker {| color = "red" |}
]
[
DataProps.Type GraphType.Bar
DataProps.X [| 1; 2; 3 |]
DataProps.Y [| 2; 5; 3 |]
]
|]
PlotProps.Layout
[
LayoutProps.Width 640
LayoutProps.Height 480
LayoutProps.Title "A Fancy Plot"
]
]
I'm a bit late to the party here, but wanted to give you a different option if you're still looking to use plotly.js with Fable.
I've been working on bindings for plotly.js for the past month or so, and it's in a pretty usable state as of now. That being said, I wouldn't say it's production ready.
This is what the example you want to convert would look like written with Feliz.Plotly:
open Feliz
open Feliz.Plotly
let chart () =
Plotly.plot [
plot.traces [
traces.scatter [
scatter.x [ 1; 2; 3 ]
scatter.y [ 2; 6; 3 ]
scatter.mode [
scatter.mode.lines
scatter.mode.markers
]
scatter.marker [
marker.color color.red
]
]
traces.bar [
bar.x [ 1; 2; 3 ]
bar.y [ 2; 5; 3 ]
]
]
plot.layout [
layout.width 320
layout.height 240
layout.title [
title.text "A Fancy Plot"
]
]
]
You can find more information out here.
I'm developing a chat application. Users will be required to sign up using a university email address.
My app will heavily rely on compartmentalizing users using a UnivID (University ID) which will be the '#' symbol along with the email domain. For example, if a user signs up using any of the following email addresses : jsmith#mail.havard.edu, jsmith#havard.edu, or jsmith#student.havard.edu. The UnivID = '#havard.edu'
I'm new to coding so I hope not to insult your intelligence if this is easy to implement.
Cheers
Here is an algorithm I created to parse the domain and extension from an email address. I used a hardcoded list of the most common domain suffixes to help ensure I don't return just the suffix part of the email if it has a 2 part domain suffix, like "co.uk" or "ca.gov".
You would just need to prepend "#" onto the resulting domain for your use case.
class func parseDomain(fromEmail email: String) -> String? {
let suffixList = ["com", "edu", "gov", "int", "mil", "net", "org", "xyz", "biz", "info", "ai", "io", "co", "ly", "eu", "cn", "ru", "nl", "de", "uk"]
guard let emailDomains = email.components(separatedBy: "#").last else {
return nil
}
let domainParts = emailDomains.components(separatedBy: ".")
guard domainParts.count >= 2 else {
return nil
}
var domain = domainParts[domainParts.count - 2] + "." + domainParts[domainParts.count - 1]
let secondLastComponent = domainParts[domainParts.count - 2]
if suffixList.contains(secondLastComponent) && domainParts.count >= 3 {
// we want to avoid returning things like co.uk, or com.au,
// so if the 2nd last component is a common suffix, return the last 3 parts instead.
domain = domainParts[domainParts.count - 3] + "." + domainParts[domainParts.count - 2] + "." + domainParts[domainParts.count - 1]
}
return domain
}
Latest 2020, Swift 5.2, XCode 11.4.1
Code that works for all inputs
let example = "email_id#gmail.com"
let components = example.components(separatedBy: "#")
if components.count > 1 {
let domain = components[1]
// domain has value gmail.com
}
I assume that you actually want everything after the ampersand. If that is the case then this code will work.
let example = "jsmith#student.harvard.edu"
let univId = "#" + example.componentsSeparatedByString("#")[1]
// "#student.harvard.edu"
If you really only want the last two parts of an extended domain string then this will work.
let example = "jsmith#student.harvard.edu"
let domain = example.componentsSeparatedByString("#")
let parts = domain.componentsSeparatedByString(".")
let univId = "#" + parts[parts.count - 2] + "." + parts[parts.count - 1]
// #harvard.edu
Web.Contents method takes Content as Binary
I use this code. It works
query = "{
""field1"" : ""value1"",
""field2"" : ""value2"",
""field3"" : {
""sub_field_3_1"" : [""value_3_1_1"", ""value_3_1_2"", ""value_3_1_1""],
""sub_field_3_2"" : [""value_3_2_1"", ""value_3_2_2"", ""value_3_2_1""]
}
}",
content = Text.ToBinary(query),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content=content
])
I understand, it is not a good workaround, because there is no reason to make double-conversions. But I could not find a way how to apply a Record, and it should look like this:
record = [
field1 = value1,
field2 = value2,
field3 = [
sub_field_3_1 = {value_3_1_1, value_3_1_2, value_3_1_1},
sub_field_3_2 = {value_3_2_1, value_3_2_2, value_3_2_1}
]
],
content = SOME_CONVERTER(record),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content = content
])
Tried to use Uri.BuildQueryString (How to POST a multipart/form-data using Power Query's Web.Contents) but it does not form Binary properly
record = [
field1 = value1,
field2 = value2,
field3 = [
sub_field_3_1 = {value_3_1_1, value_3_1_2, value_3_1_1},
sub_field_3_2 = {value_3_2_1, value_3_2_2, value_3_2_1}
]
],
content = Text.ToBinary(Uri.BuildQueryString(record)),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content=content
]
Is there some better workaround?
For now, your hard-coded JSON string is one of the better solutions.
It's less than ideal, but you could roll your own value-to-JSON transforming function like toJson:
let
record = [
field1 = "value1",
field2 = "value2",
field3 = [
sub_field_3_1 = {"value_3_1_1", null, 3.2},
sub_field_3_2 = {"value_3_2_1", "value_3_2_2", "value_3_2_1"}
]
],
toJson = (v as any) as text =>
if v is null then "null" else
if v is logical or v is number then Text.From(v) else
if v is text then """" & Text.Replace(Text.Replace(v, "\", "\\"), """", "\""") & """" else
if v is list then "[" & Text.Combine(List.Transform(v, #toJson), ", ") & "]" else
if v is record then "{" &
Text.Combine(List.Transform(
Record.FieldNames(v),
(n) => #toJson(n) & ": " & #toJson(Record.Field(v, n))), ", ")
& "}" else
error "not implemented",
jsonText = toJson(record)
in
jsonText
Some deficiencies compared to what a real Json.FromValue library function should do:
only primitive text escaping
see json.org for all the special characters you'd need to escape
doesn't handle cyclic M values, special not-numbers, or other types of value types
will choke on very large values (string concat will use a lot of memeory)
SOME_CONVERTER == Json.FromValue
let
record = [
field1 = "value1",
field2 = "value2",
field3 = [
sub_field_3_1 = {"value_3_1_1", "value_3_1_2", "value_3_1_1"},
sub_field_3_2 = {"value_3_2_1", "value_3_2_2", "value_3_2_1"}
]
],
content = Json.FromValue(record),
web=Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content = content
])
in
web