Creating map function in Red language - higher-order-functions

How can I create map, a higher order function, in Red language. It should take a block and a function as arguments and apply the sent function to each member of block. I tried following code:
Red []
mapfn: function[blk sfn][
outblk: copy []
foreach i blk[
append outblk (sfn i) ]
outblk ]
; to test:
myblk: [" this " " is " " a " " line " "for" " testing " " only "]
probe mapfn myblk 'reverse
probe mapfn myblk 'trim
But it is not working - it simply sends back the original block without changing it or giving any error message. How can this be corrected?

In Rebol you have the mezzanine function apply
>> help apply
USAGE:
APPLY func block /only
DESCRIPTION:
Apply a function to a reduced block of arguments.
APPLY is a function value.
ARGUMENTS:
func -- Function value to apply (Type: any-function)
block -- Block of args, reduced first (unless /only) (Type: block)
REFINEMENTS:
/only -- Use arg values as-is, do not reduce the block
(SPECIAL ATTRIBUTES)
throw
See source apply.
As long as Red has no native apply you can write your own mapping function e.g
mapfn: function[blk sfn][
outblk: copy []
foreach i blk[
append outblk sfn copy i
]
outblk
]
Get the function with :functionname
>> myblk: [" this " " is " " a " " line " "for" " testing " " only "]
== [" this " " is " " a " " line " "for" " testing " " only "]
>> probe mapfn myblk :reverse
[" siht " " si " " a " " enil " "rof" " gnitset " " ylno "]
== [" siht " " si " " a " " enil " "rof" " gnitset " " ylno "]
>> probe mapfn myblk :trim
["this" "is" "a" "line" "for" "testing" "only"]
== ["this" "is" "a" "line" "for" "testing" "only"]
>>
An alternative and better way as you can not copy all datatypes is e.g.
mapfn: function[blk sfn][
collect [
foreach i blk[
keep sfn i
]
]
]
and call the function this way if no do not want to modify the original
mapfn newblk: copy/deep myblk :reverse

Related

FIlter a value from stdout_lines of Ansible output below

I would like to FIlter value of Endpoint from stdout_lines of Ansible output below:
"{",
" \"DBCluster\": {",
" \"MasterUsername\": \"mstrusr\", ",
" \"ReaderEndpoint\": \"tstng4.cluster-ro-czzpyhxnqlzh.us-east-1.docdb.amazonaws.com\", ",
" \"VpcSecurityGroups\": [",
" {",
" \"Status\": \"active\", ",
" \"VpcSecurityGroupId\": \"sg-xxx5677\"",
" }",
" ], ",
" \"HostedZoneId\": \"ZNKXH85TT8WVW\", ",
" \"Status\": \"creating\", ",
" \"MultiAZ\": false, ",
" \"PreferredBackupWindow\": \"03:51-04:21\", ",
" \"DBSubnetGroup\": \"subnet\", ",
" \"BackupRetentionPeriod\": 1, ",
" \"PreferredMaintenanceWindow\": \"mon:05:00-mon:05:30\", ",
" \"Engine\": \"docdb\", ",
" \"Endpoint\": \"tstng4.cluster-czzpyhxnqlzh.us-east-1.docdb.amazonaws.com\", ",
" \"ClusterCreateTime\": \"2020-01-23T10:06:44.338Z\", ",
" \"EngineVersion\": \"3.6.0\", ",
" \"DeletionProtection\": true, ",
" \"DBClusterIdentifier\": \"vce-docdb-tst4\", ",
" \"DbClusterResourceId\": \"cluster-E3ICGSCJRPWDY7NRQTELXFCKTQ\", ",
" \"DBClusterMembers\": [], ",
" \"Port\": 27017, ",
" \"StorageEncrypted\": false, ",
" \"AssociatedRoles\": [], ",
" \"DBClusterParameterGroup\": \"default.docdb3.6\", ",
" \"AvailabilityZones\": [",
" \"us-east-1a\", ",
" \"us-east-1b\", ",
" \"us-east-1c\"",
" ], ",
" \"DBClusterArn\": \"arn:aws:rds:us-east-1:570346948435:cluster:vce-docdb-tst4\"",
" }",
I am trying to use below filter to get value of Endpoint:
stdout_lines.DBCluster.Endpoint
Your result is the json object which have to be piped through from_json filter. Simple test:
- hosts: masters
gather_facts: false
tasks:
- shell: /root/ansible/test.sh
register: result
- debug:
msg: "{{ (result.stdout | from_json).DBCluster.Endpoint }}"
Test data:
#!/bin/bash
echo '{'
echo '"DBCluster": {'
echo '"Endpoint": "value1"'
echo '}, "key2": "value2"'
echo '}'

Unable To Send Multiline Plaintext Email With Mailgun using Ruby

I am using two files to send messages with Mailgun. They are:
email_sender.rb
message_text.rb
The code for the first one is:
require './message_text.rb'
fromLabel = "Email Guy"
fromAddress = "digital#mail.*****.com"
toAddress = "info#*****.net"
subject = "An Invitation"
cmd = "curl -s --user 'api:key-*****' https://api.mailgun.net/v3/mail.*****.com/messages -F from='" + fromLabel + " <" + fromAddress + ">' -F to='" +toAddress + "' -F subject='" + subject + "' -F text='" + $message + "'"
wasGood = system (cmd)
The code for the second file is:
$message = "Line One Text."
+ "\n" + "\n" + "And Line Two Text!"
When I test sending an email, the message that arrives in my test account inbox is as follows.
Line One Text.
If you run the code with ruby -w, that is: with warnings enabled, it warns: warning: possibly useless use of + in void context, with the according line number, pointing to:
$message = "Line One Text."
+ "\n" + "\n" + "And Line Two Text!"
Which is a polite way of Ruby saying: "well, it's not a syntax error, but it does not make sense to me."
Try it with
$message = "Line One Text.
And Line Two Text!" # or: "Line One Text.\n\nAnd Line Two Text!"
So I got it working by putting everything on a single line.
$message = "Line One Text!" + "\n" + "\n" + "Line Two Text!"

Expected "End" Code

I am getting this error for the code below on the second to last line Char 13. I cannot see any problem with the code. Is someone able to help?
Function Results(KBname)
Select Case Return
Case 9009
LogFile.WriteLine(Now & " - WARNING: " & KBname & " is already installed; skipping installation.")
Case 2359302
LogFile.WriteLine(Now & " - WARNING: " & KBname & " is already installed; skipping installation.")
Case -2145124329
LogFile.WriteLine(Now & " - WARNING: " & KBname & " is not required for this system; skipping installation.")
Case Else
LogFile.WriteLine(Now & " - Install of " & KBname & " has completed with return code: " & RETURN)
End Select
End Function
If is error code 800A03F6 it could mean an error related to an If-Then elsewhere (prior to this function) that wasn't properly closed.

How to split string by comma except the comma in double quote string

I want to split the following data by comma except the comma in double quote string
"(CONTRACTS OF 5,000 BUSHELS)"
How to do it ?
raw_str.split doesn't work
raw_str = '"WHEAT-SRW " ,150106 ,
CBT ,00 ,001 ,
31.5, 26.4, "(CONTRACTS OF 5,000 BUSHELS)"'
Actually I want to parse the file from "http://www.cftc.gov/dea/newcot/f_disagg.txt"
html = Curl.get("http://www.cftc.gov/dea/newcot/f_disagg.txt")
html.body_str.split ~~~~
Why reïnvent the wheel ?
Your text is CSV, so use the CSV gem like this
require 'csv'
CSV.foreach("f_disagg.txt", {:headers=>false, :col_sep => ",", force_quotes: false, :quote_char => "\x00"}) do |row|
p row
end
["\"WHEAT-SRW - CHICAGO BOARD OF TRADE\" ", "150106 ", "2015-01-06", "001602 ", "CBT ", "00 ", "001 ", " 375872", " 32141", " 138392", " 111530", " 5662", " 14877", " 92842", " 75279", " 34611", " 34276", " 44861", " 22692", " 342969", " 336374", " 32903", " 39498", " 260179", " 19786", " 85222", " 93057", " 6641", " 1023", " 67360", " 78720", " 16703", " 36845", " 42816", " 4110", " 238884", " 235235", " 21295", " 24944", " 115693", " 12355", " 53170", " 21065", " 1613", " 11262", " 34424", " 5501", " 8966", " 5915", " 10529", " 10098", " 104085", " 101139", " 11608", " 14554", " 3895", " -1034", " -7666", " -763", " 225", " 312", " -4547", " 2110", " 6077", " -334", " -435", " 1750", " 1461", " 2373", " 2434", " 1522", " 100.0", " 8.6", " 36.8", " 29.7", " 1.5", " 4.0", " 24.7", " 20.0", " 9.2", " 9.1", " 11.9", " 6.0", " 91.2", " 89.5", " 8.8", " 10.5", " 100.0", " 7.6", " 32.8", " 35.8", " 2.6", " 0.4", " 25.9", " 30.3", " 6.4", " 14.2", " 16.5", " 1.6", " 91.8", " 90.4", " 8.2", " 9.6", " 100.0", " 10.7", " 46.0", " 18.2", " 1.4", " 9.7", " 29.8", " 4.8", " 7.7", " 5.1", " 9.1", " 8.7", " 90.0", " 87.4", " 10.0", " 12.6", " 353", " 63", " 84", " 21", " 5", " 16", " 69", " 35", " 43", " 45", " 66", " 67", " 269", " 261", " 340", " 45", " 79", " 21", " 5", " .", " 66", " 33", " 31", " 54", " 63", " 38", " 215", " 225", " 222", " 37", " 68", " 17", " 6", " 14", " 33", " 13", " 12", " 23", " 46", " 29", " 144", " 161", " 16.2", " 12.2", " 26.0", " 21.6", " 14.6", " 10.9", " 24.0", " 18.5", " 18.4", " 14.9", " 30.4", " 25.7", " 18.2", " 14.9", " 29.9", " 25.6", " 23.6", " 19.4", " 37.6", " 32.5", " 21.7", " 17.7", " 31.5", " 26.4", "\"(CONTRACTS OF 5", "000 BUSHELS)\" ", "\"001602\" ", "\"CBT\" ", "\"001\" ", "\"A10\" ", "\"FutOnly\""], etc
EDIT: here a version that deals with the comma between parentheses, if a ) is found then the field is joined with the precious and the field it self deleted
CSV.foreach("f_disagg.txt", {:headers=>false, :col_sep => ",", force_quotes: false, :quote_char => "\x00"}) do |row|
row.each_with_index do |val, index|
if val[')']
row[index-1] = "#{row[index-1]},#{val}"
row.delete_at(index)
val = ''
end
val
end
p row
end
You can accomplish this using regex and "negative lookahead" and "negative lookbehind"[0]:
raw_str = '"WHEAT-SRW " ,150106 ,
CBT ,00 ,001 ,
31.5, 26.4, "(CONTRACTS OF 5,000 BUSHELS)"'
raw_str.split(/(,)(?<!")(!?=")/)
["\"WHEAT-SRW \" ,150106 ,2015-01-06,001602 \n ,CBT ,00 ,001 , 375872, 32141,
14.9, \n37.6, 32.5, 21.7, 17.7, \n31.5, 26.4,
\"(CONTRACTS OF 5,000 BUSHELS)\""]
You're saying: "put the comma in a capture group, only if it wasn't preceded by a double quote and if it's not followed by a double quote."
[0] http://ruby-doc.org/core-2.2.0/Regexp.html#class-Regexp-label-Anchors

Load Knockout observableArray with server Json populate only one element

here I am developing an application that retrieve data in a server via jquery ajax with then I do the Databindings with knockout
The data json well pass to my scripts the only the problem is that in my html table only one record appears
here my json as given by the server:
Json returned by the server:
{
"Id": "4 ",
" key_0 ": " 4 ",
" Nom_Agent ": " Grace ",
" key_1 ": " Grace ",
" PNom_Agent ": " Malulu ",
" key_2 ": " Malulu "
" Sexe_Agent ",
" M ",
" key_3 ",
" M ",
" Adresse_Agent ",
" 2 ",
" key_4 ",
" 2 ",
" Telephone_Agent ": " 243 900 100 115 ",
" key_5 ": " 243 900 100 115 "
" Libelle_Role ": " Cashier ",
" key_6 ": " Cashier ",
" Email_Agent ": " gracemalulu#gmail.com ",
" key_7 ": " gracemalulu#gmail.com ",
" Nom_Agence ": " KIN 02 "
" key_8 ": " 02 KIN ",
" Libelle_Caisse ": "",
" key_9 ": "",
" Id_Agence ",
" 2",
" key_10 ",
" 2",
" role_id ",
" 2",
" key_11 "
" 2",
" Id_Caisse ": " ",
" key_12 ": "",
"Password ": " 7f59e02e7deaa6a33004b25a86024dee ",
" key_13 ": " 7f59e02e7deaa6a33004b25a86024dee ",
" Username": " gmalulu ",
" key_14 ": " gmalulu "
" Active ": " 1",
" key_15 ": " 1 "
}, {
" Id", " 2", " key_0 ", " 2", " Nom_Agent "
" Vasco ", " key_1 "
" Vasco ", " PNom_Agent "
" Kabangu "
" key_2 "
" Kabangu ", " Sexe_Agent ", " M ", " key_3 ", " M ", " Adresse_Agent "
" iSC ", " key_4 "
" iSC ", " Telephone_Agent ": " 243 ",
" key_5 ": " 243 ",
" Libelle_Role ": " Agency head ",
" key_6 ": " Agency head ",
" Email_Agent ": " vkabungu # ",
" key_7 ": " vkabungu # ",
" Nom_Agence ": " KIN 02 ",
" key_8 ": " KIN 02 ",
" Libelle_Caisse ": " ",
" key_9 ": " ",
" Id_Agence ",
" 2 ",
" key_10 ",
" 2 ",
" role_id ",
" 3 ",
" key_11 ",
" 3 ",
" Id_Caisse ": " ",
" key_12 ": " ",
" Password ": " 325a2cc052914ceeb8c19016c091d2ac ",
" key_13 ": " 325a2cc052914ceeb8c19016c091d2ac ",
" Username ": " vkabungu ",
" key_14 ": " vkabungu ",
" Active ": " 1 ",
" key_15 "
" 1 "
}, {
" Id", " 1", " key_0 ": " 1",
" Nom_Agent "
" Lepeya "
" key_1 "
" Lepeya "
" PNom_Agent "
" Otoko "
" key_2 "
" Otoko ",
" Sexe_Agent ",
" M ",
" key_3 ",
" M ",
" Adresse_Agent "
" lol122 Mombele "
" key_4 "
" lol122 Mombele "
" Telephone_Agent ": " 213 ",
" key_5 ": " 213 "
" Libelle_Role ": " Cashier ",
" key_6 ": " Cashier ",
" Email_Agent ": " lepeyaherve#agb.cd ",
" key_7 ": " lepeyaherve#agb.cd ",
" Nom_Agence ": " KIN 02 "
" key_8 ": " 02 KIN ",
" Libelle_Caisse "
" Case 01 ",
" key_9 "
" Case 01 ",
" Id_Agence ",
" 2",
" key_10 ",
" 2",
" role_id "
" 2"
" key_11 ": " 2",
" Id_Caisse ": " 2",
" key_12 ": " 2",
" Password ": " 325a2cc052914ceeb8c19016c091d2ac ",
" key_13 ": " 325a2cc052914ceeb8c19016c091d2ac ",
" Username": " lherve "
" key_14 ": " lherve ",
" Active ": " 0 ",
" key_15 ": " 0 "
}]
the script knockout:
<script type="text/javascript">
function Agent(data) {
this.id=data.id;
this.Nom_Agent=data.Nom_Agent;
this.PNom_Agent=data.PNom_Agent;
this.Sexe_Agent=data.Sexe_Agent;
this.Adresse_Agent=data.Adresse_Agent;
this.Telephone_Agent=data.Telephone_Agent;
this.Libelle_Role=data.Libelle_Role;
this.Email_Agent=data.Email_Agent;
this.Nom_Agence=data.Nom_Agence;
this.Username=data.Username;
this.Id_Role=data.Id_Role;
this.Id_Caisse=data.Id_Caisse;
this.Date_Affectation=data.Date_Affectation;
this.Libelle_Caisse=data.Libelle_Caisse;
this.Id_Lieu=data.Id_Lieu;
this.Active=ko.observable(data.Active)
}
function AgentListViewModel() {
// Data
var self = this;
self.agents= ko.observableArray([]);
$.ajax("/agence/allagent", {
contentType: "application/json",
success: function(result) {
var mappedAgents = $.map( result, function(item) {
return new Agent(item) ; });
self.agents(mappedAgents);
});
}
ko.applyBindings(new AgentListViewModel());
</script>
please help me
you can set the observable array using the knockout mapping plugin
AgentListViewModel function ( ) {
var self = this;
self.agents ko.observableArray = ( [ ] ) ;
$.ajax ("/branch/allagent" {
contentType : "application/json "
success : function ( result) {
ko.mapping.fromJS(result, {}, self.agents) };
}
});
}
I set up a fiddle with an example of how you can use it.
Example

Resources