I am working on a Problem in Maple. But the question i have is general and could be answered based on ANY programming Language.
The Problem:
Use only map function (no loops). Assume there are two lists A and B with n and m elements. replace those elements in A with "true", which are also found in B and replace the other elements in A with "false".
The question:
i did try to solve this problem like that:
funk := proc(i::integer,j::integer) # checks if two elements are identical
if (i = j) then
return true;
else
return false;
end if;
end proc:
funktion := proc(int::integer, L :: list) # calls funk on every element of L.
map(funk, L, int);
end proc:
SchnittTrueA := proc(M::list, L::list) # calls funktion on every Element of M and L.
map(funktion,M,L);
end proc:
And if i run SchnittTrueA(k,l); for k :=[1,3] and l := [2,3] i get this result:
[[false, false], [false, true]]
That is by no mean what i want the Program to do!
i need something like that [false, true]
I did try everything i could but i have no idea how to solve this problem.
P.S1: i am a newbie programmer so please do not be hard on me :D.
P.S2: If you want to give me any Tip / Answer, it does not need to be in Maple Language as long as you can use map function.
It's not clear what else you may use, besides map. Eg. your user-defined procedure uses if..then, etc.
But the following don't use loops.
The first two are reasonably efficient in the sense that for each element t in list A they generate the true as soon as any matching element is found in list B.
In contrast the last two approaches are inefficient because they test each element t in list A against every element in list B (even if a match has already been found).
restart;
A := [$1..10];
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
B := [seq(ithprime(i),i=1..4)];
[2, 3, 5, 7]
map(member,A,B);
[false, true, true, false, true, false, true, false, false, false]
map(t->ormap(evalb#`=`,B,t),A);
[false, true, true, false, true, false, true, false, false, false]
map(t->`or`(op(map(evalb#`=`,B,t))),A);
[false, true, true, false, true, false, true, false, false, false]
map(t1->`if`(map(t2->`if`(t2=t1,true,NULL),B)=[true],
true,false),A);
[false, true, true, false, true, false, true, false, false, false]
Of the four apporaches shown above, the fourth is closest in spirit to your attempt.
I'll show some equivalents to the fourth above, each getting closer to your original attempt.
restart;
A := [$1..10]:
B := [seq(ithprime(i),i=1..4)]:
Each of these equivalents use those same A and B example list.
func := (t1,t2)->`if`(t2=t1,true,NULL):
funktion := (t1,B)->`if`(map(func,B,t1)=[true],
true,false):
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
Now using proc instead of shorter operator syntax.
func := proc(t1,t2)
`if`(t2=t1,true,NULL);
end proc:
funktion := proc(t1,B)
`if`(map(func,B,t1)=[true],
true,false);
end proc:
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
Now using long-form of if..then, instead of shorter if operator form.
func := proc(t1,t2)
if t2=t1 then
true;
else
NULL;
end if;
end proc:
funktion := proc(t1,B)
if map(func,B,t1)=[true] then
true;
else
false;
end if;
end proc:
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
I've looked at a few other related questions and I still can't seem to find what I'm looking for. This is an example JSON payload being sent to an API I'm writing:
{
"publishType": "Permitable",
"electricalPanelCapacity": 0.0,
"roofConstruction": "Standard/Pitched",
"roofType": "Composition Shingle",
"systemConstraint": "None",
"addedCapacity": 0.0,
"isElectricalUpgradeRequired": false,
"cadCompletedBy": "94039",
"cadCompletedDate": "2017-02-01T02:18:15Z",
"totalSunhourDeficit": 5.0,
"designedSavings": 5.0,
"isDesignedWithinTolerance": "N/A",
"energyProduction": {
"january": 322.40753170051255,
"february": 480.61501312589826,
"march": 695.35215022905118,
"april": 664.506907341219,
"may": 877.53769491124172,
"june": 785.56924358327,
"july": 782.64347308783363,
"august": 760.1123565793057,
"september": 574.67050827435878,
"october": 524.53797441350321,
"november": 324.31132291046379,
"december": 280.46921069200033
},
"roofSections": [{
"name": "North East Roof 4",
"roofType": "Composition Shingle",
"azimuth": 55.678664773137086,
"tilt": 15.0,
"solmetricEstimate": 510.42831656979456,
"shadingLoss": 14.0,
"systemRating": 580.0,
"sunHours": 0.88004882167205956,
"moduleCount": 2,
"modules": [{
"moduleRating": 290.0,
"isovaPartNumber": "CDS-MON-007070",
"partCount": 2
}]
}, {
"name": "South West Roof 3",
"roofType": "Composition Shingle",
"azimuth": 235.67866481720722,
"tilt": 38.0,
"solmetricEstimate": 3649.1643776261653,
"shadingLoss": 59.0,
"systemRating": 5220.0,
"sunHours": 0.69907363556056812,
"moduleCount": 18,
"modules": [{
"moduleRating": 290.0,
"isovaPartNumber": "CDS-MON-007070",
"partCount": 18
}]
}, {
"name": "South East Roof",
"roofType": "Composition Shingle",
"azimuth": 145.67866477313709,
"tilt": 19.0,
"solmetricEstimate": 2913.1406926526984,
"shadingLoss": 31.0,
"systemRating": 2900.0,
"sunHours": 1.0045312733285168,
"moduleCount": 10,
"modules": [{
"moduleRating": 290.0,
"isovaPartNumber": "CDS-MON-007070",
"partCount": 10
}]
}],
"SystemConfiguration": {
"inverters": [{
"isovaPartNumber": "ENP-INV-007182",
"partCount": 30
}]
}
}
Describing all the beginning parameters was easy.
/post/new-cad/{serviceNumber}:
post:
summary: Publish a new CAD record.
description: Creates a new CAD record under the provided service number and returns the name of the new CAD record, the unique SF ID, and the deep link URL for Salesforce.
parameters:
- name: serviceNumber
in: path
description: The service number for the solar project you're interested in publishing to.
required: true
type: string
- name: publishType
in: formData
description: The type of CAD record to publish (Proposal, Permitable, or AsBuilt).
required: true
type: string
- name: electricalPanelCapacity
in: formData
required: true
type: number
format: double
- name: roofConstruction
in: formData
description: New, Flat Roof, Open Beam, Standard/Pitched
required: true
type: string
- name: roofType
in: formData
description: Composition Shingle, Membrane (Rubber, TPO, PVC, EPDM), Metal - Corrugated (S-Curve), Metal - Standing Seam, Metal - Trapezoidal, Multi Roof Type, Rolled Comp, Silicone, Tar & Gravel, Tile - Flat, Tile - S-Curve, or Tile - W-Curve
type: string
- name: systemConstraint
in: formData
description: Usage, None, Roof, Electrical, Shading, or 10kW Max
required: true
type: string
- name: addedCapacity
in: formData
required: true
type: number
format: double
- name: isElectricalUpgradeRequired
in: formData
type: boolean
- name: cadCompletedBy
in: formData
description: Employee ID of record author.
type: number
required: true
- name: cadCompletedDate
in: formData
description: The date the CAD record was completed.
type: string
format: date
required: true
- name: totalSunhourDeficit
in: formData
type: number
format: double
- name: designedSavings
in: formData
type: number
format: double
- name: isDesignedWithinTolerance
in: formData
type: string
description: Yes, No, or N/A
And yields the expected result in Swagger-UI:
But now I'm struggling with the last parts of the example JSON payload above. I'm unsure how to express the energyProduction key which is an object with a key for each month of the year. I'm also unsure how to describe roofSections which is an array of objects and systemConfiguration which is an object with a property inverters whose value is an array of objects.
I'm going over the swagger documentation quite a bit but I'm still pretty confused and hoping maybe someone here can explain things a little better to me.
I figured it out. Turns out formData is not what I should have been using for my parameters. Instead I needed to use body and define the structure of the JSON that would populate the body. Here is the completed design file using a body parameter with an object schema and describes all the nested objects and arrays as well.
/new-cad/{serviceNumber}:
post:
summary: Publish a new CAD record.
description: Creates a new CAD record under the provided service number and returns the name of the new CAD record, the unique SF ID, and the deep link URL for Salesforce.
parameters:
- name: serviceNumber
in: path
description: The service number for the solar project you're interested in publishing to.
required: true
type: string
- name: cadData
in: body
description: A JSON payload containing the data required to publish a new CAD record.
required: true
schema:
type: object
properties:
publishType:
type: string
default: "Proposal"
enum: ["Proposal","Permitable","AsBuilt"]
electricalPanelCapacity:
type: number
roofConstruction:
type: string
default: "New"
enum: ["New","Flat Roof","Open Beam","Standard/Pitched"]
roofType:
type: string
enum: ["Composition Shingle","Membrane (Rubber, TPO, PVC, EPDM)","Metal - Corrugated (S-Curve)","Metal - Standing Seam","Metal - Trapezoidal","Multi Roof Type","Rolled Comp","Silicone","Tar & Gravel","Tile - Flat","Tile - S-Curve","Tile - W-Curve"]
systemConstraint:
type: string
default: "None"
enum: ["None","Usage","Roof","Electrical","Shading","10kW Max"]
addedCapacity:
type: number
default: 0
isElectricalUpgradeRequired:
type: boolean
cadCompletedBy:
type: string
cadCompletedDate:
type: string
totalSunhourDeficit:
type: number
designedSavings:
type: number
isDesignedWithinTolerance:
type: string
default: "N/A"
enum: ["N/A","Yes","No"]
energyProduction:
type: object
properties:
january:
type: number
february:
type: number
march:
type: number
april:
type: number
may:
type: number
june:
type: number
july:
type: number
august:
type: number
september:
type: number
october:
type: number
november:
type: number
december:
type: number
roofSections:
type: array
items:
type: object
properties:
name:
type: string
roofType:
type: string
enum: ["Composition Shingle","Membrane (Rubber, TPO, PVC, EPDM)","Metal - Corrugated (S-Curve)","Metal - Standing Seam","Metal - Trapezoidal","Multi Roof Type","Rolled Comp","Silicone","Tar & Gravel","Tile - Flat","Tile - S-Curve","Tile - W-Curve"]
azimuth:
type: number
tilt:
type: number
solmetricEstimate:
type: number
shadingLoss:
type: number
systemRating:
type: number
sunHours:
type: number
moduleCount:
type: integer
modules:
type: array
items:
type: object
properties:
moduleRating:
type: number
isovaPartNumber:
type: string
partCount:
type: integer
systemConfiguration:
type: object
properties:
inverters:
type: array
items:
type: object
properties:
isovaPartNumber:
type: string
partCount:
type: integer
tags:
- NEW-CAD
responses:
200:
description: CAD record created successfully.
schema:
type: object
properties:
cadName:
type: string
sfId:
type: string
sfUrl:
type: string
examples:
cadName: some name
sfId: a1o4c0000000GGAQA2
sfUrl: http://some-url-to-nowhere.com
204:
description: No project could be found for the given service number.
500:
description: Unexpected error. Most likely while communicating with Salesforce.
schema:
type: string
So now I can still get the serviceNumber from the path while everything else comes in the request body. One thing to keep in mind here is that you cannot use all the same Swagger Data Types. For example I tried to use double for one of the properties and Swagger complained that it couldn't parse type double. I was very confused until I finally found the section of the docs describing the difference between formData parameters and a body parameter (of which you can only have one, because it describes the entire request body). Basically you can only use data types that are supported by the JSON schema.
Swagger-UI now shows a single textarea instead of multiple input fields for each parameter. Not as pretty but it works great. You can click the "Example Value" box on the right and it places a predefined JSON template in the textarea for you so you can just fill in the values.
If you are just learning Swagger like I am I hope this helps!
I have a jqGrid with below definintion
function loadSearchGrid(jsonData){
jQuery("#searchGrid").jqGrid({
datatype: 'jsonstring',
width: w ,
datastr: jsonData,
colNames:['Hidden Location ID','Hidden Contact ID','Hidden Customer ID','ID','Company','Service Address', 'State', 'LCON First','LCON Last','LCON Phone','LCON Email','LCON External ID','Letters','Notes','Attachments'],
colModel:[
{name: "hiddenLocationID",index:'hiddenLocationID', sortable:false, search:false, width:110, hidden:true},
{name: "hiddenContactID",index:'hiddenContactID', sortable:false, search:false, width:110, hidden:true},
{name: "hiddenCustomerID",index:'hiddenCustomerID', sortable:false, search:false, width:110, hidden:true},
{name: "ID",index:'ID', sortable:true, search:false, width:10},
{name:'company',index:'company', width:40,search:true, sorttype:"int"},
{name:'serviceAddress',index:'serviceAddress',search:true, width:40},
{name:'state',index:'state',search:true, width:40},
{name:'lconFirst',index:'lconFirst',search:true, width:40, align:"center"},
{name:'lconLast',index:'lconLast',search:true, width:40, align:"center"},
{name:'lconPhone',index:'lconPhone',search:true, width:40,align:"center"},
{name:'lconEmail',index:'lconEmail',search:true, width:40,align:"center",formatter:emailFormatter},
{name:'lconExternalId',index:'lconExternalId',search:true, width:40,align:"center",formatter:lconExternalIdFormatter },
{name:'letters',index:'letters', width:40,search:false,align:"center",formatter:letterFormatter},
{name:'notes',index:'notes', width:40,search:false,align:"center",formatter:noteFormatter},
{name:'attachments',index:'attachments', width:40,search:false, sortable:false,formatter:attachmentFormatter}
],
loadComplete: function ()
{
var totalRecords = jQuery("#searchGrid").getGridParam("records");
/*if (totalRecords==0)
{
$("#searchDiv").addClass("tblSection marT10 nodataFound");
$("#searchDiv").html("No Results were found matching your criteria");
}*/
//$('#searchGrid').jqGrid('filterToolbar',{searchOnEnter:false , defaultSearch:"cn"});
},
onSelectRow: function(rowid, iRow, iCol, e)
{
var company = $('#searchGrid').jqGrid('getCell', rowid, 'company');
var serviceAddress = $('#searchGrid').jqGrid('getCell', rowid, 'serviceAddress');
var locationId = $('#searchGrid').jqGrid('getCell', rowid, 'hiddenLocationID');
var contactId = $('#searchGrid').jqGrid('getCell', rowid, 'hiddenContactID');
var customerId = $('#searchGrid').jqGrid('getCell', rowid, 'hiddenCustomerID');
$('#customerId').val(customerId);
$('#contactId').val(contactId);
$('#locationId').val(locationId);
//disbaled top buttons
$("#exportToExcel").removeAttr("disabled");
$("#editContact").removeAttr("disabled");
$("#deleteContact").removeAttr("disabled");
$("#newLcon").removeAttr("disabled");
$("#newLocation").removeAttr("disabled");
},
rowNum:10,
rowList:[10,20,30],
pager: '#searchPager',
paging:true,
height: 400,
sortname: 'company',
viewrecords: true,
sortorder: "desc",
gridview: true,
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
id:"ID",
repeatitems: false
},
loadonce: false,
ignoreCase: true
});
console.log("msg is ", jsonData);
$('#searchGrid').jqGrid('filterToolbar',{searchOnEnter:false , defaultSearch:"cn"});}
I get the json data in the following format from Struts2 Action:
Also my jsonData is as below
{"total":"18","page":"1","lastRecord":"18","cspUserReqFormUrl":"http://ebiz.sbc.com/cpct/cpct_user_request_form.cfm","records":"18","popCount":"5","rows":[{"lconEmail":"111#111.com","hiddenCustomerID":"119289","state":"Texas","lconPhone":"7133868166","lconLast":"Rapp","hiddenContactID":"95696","lconExternalId":"11#111.com|Remove","hiddenLocationID":"97459","company":"AT T","ID":"1","attachments":"0.0","lconFirst":"Loren","notes":"1.0","letters":"0.0","serviceAddress":"11830 WEBB CHAPEL RD, DALLAS, Texas,75234"},{"lconEmail":"111#111.COM","hiddenCustomerID":"37302","state":"Texas","lconPhone":"9727151699","lconLast":"FLOYD","hiddenContactID":"62753","lconExternalId":"|Add","hiddenLocationID":"63956","company":"Prudential","ID":"2","attachments":"0.0","lconFirst":"MARK","notes":"1.0","letters":"0.0","serviceAddress":"5001 SPRING VALLEY RD, FARMERS BRANCH, Texas,75244"},{"lconEmail":"111#111.COM","hiddenCustomerID":"111313","state":"Texas","lconPhone":"9724372888","lconLast":"SAVARIAN","hiddenContactID":"85208","lconExternalId":"|Add","hiddenLocationID":"86708","company":"ATX-First Community Bank N.A.","ID":"3","attachments":"0.0","lconFirst":"SHELLY","notes":"1.0","letters":"0.0","serviceAddress":"1755 N. COLLINS BLVD, RICHARDSON, Texas,75080"},{"lconEmail":"111#111.COM","hiddenCustomerID":"111711","state":"Texas","lconPhone":"2672628167","lconLast":"COREY","hiddenContactID":"82922","lconExternalId":"|Add","hiddenLocationID":"84302","company":"AT&T-VIEWPOINT BANK","ID":"4","attachments":"0.0","lconFirst":"JAIME","notes":"1.0","letters":"0.0","serviceAddress":"1001 E. CAMPBELL RD, RICHARDSON, Texas,75081"},{"lconEmail":"111#111.COM","hiddenCustomerID":"11025","state":"Texas","lconPhone":"8475079311","lconLast":"CALLAGHAN","hiddenContactID":"63333","lconExternalId":"|Add","hiddenLocationID":"64534","company":"Apple","ID":"5","attachments":"0.0","lconFirst":"BILL","notes":"1.0","letters":"0.0","serviceAddress":"8787 N. CENTRAL EXPWY, DALLAS, Texas,75225"},{"lconEmail":"111#111.COM","hiddenCustomerID":"111290","state":"Texas","lconPhone":"2147018465","lconLast":"KIM","hiddenContactID":"82552","lconExternalId":"|Add","hiddenLocationID":"83932","company":"QCC DBA CENTURYLINK QCC-SAMSUNG SDS AMERICA INC","ID":"6","attachments":"0.0","lconFirst":"TONY","notes":"1.0","letters":"0.0","serviceAddress":"1301 E LOOKOUT DR, RICHARDSON, Texas,75082"},{"lconEmail":"111#111.COM","hiddenCustomerID":"113889","state":"Texas","lconPhone":"2142779863","lconLast":"DAO","hiddenContactID":"85110","lconExternalId":"|Add","hiddenLocationID":"86594","company":"ATX-VIETFACE TV HOUSTON","ID":"7","attachments":"0.0","lconFirst":"HUNG","notes":"1.0","letters":"0.0","serviceAddress":"1301 E. ARAPAHO RD, RICHARDSON, Texas,75081"},{"lconEmail":"111#111.COM","hiddenCustomerID":"108914","state":"Texas","lconPhone":"9725810403","lconLast":"REAVES","hiddenContactID":"80035","lconExternalId":"|Add","hiddenLocationID":"81398","company":"QCC DBA CENTURYLINK QCC-PILGRIMS PRIDE","ID":"8","attachments":"0.0","lconFirst":"STEPHAN","notes":"1.0","letters":"0.0","serviceAddress":"1780 JAY ELL DR, RICHARDSON, Texas,75081"},{"lconEmail":"111#111.COM","hiddenCustomerID":"91749","state":"Texas","lconPhone":"2543156520","lconLast":"BLACK","hiddenContactID":"62652","lconExternalId":"|Add","hiddenLocationID":"63853","company":"SMITHFIELD","ID":"9","attachments":"0.0","lconFirst":"JASON","notes":"1.0","letters":"0.0","serviceAddress":"17201 WATERVIEW PKWY, DALLAS, Texas,75252"},{"lconEmail":"111#111.COM","hiddenCustomerID":"103403","state":"Texas","lconPhone":"9726640727","lconLast":"HAWORTH","hiddenContactID":"74404","lconExternalId":"|Add","hiddenLocationID":"75724","company":"ATX-AWARD SOLUTIONS INC","ID":"10","attachments":"0.0","lconFirst":"BOB","notes":"1.0","letters":"0.0","serviceAddress":"2100 LAKESIDE BLVD, RICHARDSON, Texas,75082"},{"lconEmail":"111#111.COM","hiddenCustomerID":"115783","state":"Texas","lconPhone":"9727293352","lconLast":"SELLERS","hiddenContactID":"86985","lconExternalId":"|Add","hiddenLocationID":"88505","company":"QCC DBA CENTURYLINK QCC-WAL MART","ID":"11","attachments":"0.0","lconFirst":"GLEN","notes":"1.0","letters":"0.0","serviceAddress":"400 INTERNATIONAL PKWY, RICHARDSON, Texas,75081"},{"lconEmail":"111#111.COM","hiddenCustomerID":"21501","state":"Texas","lconPhone":"9727929393","lconLast":"MARTINEZ","hiddenContactID":"86997","lconExternalId":"|Add","hiddenLocationID":"88518","company":"ATX-HILTON","ID":"12","attachments":"0.0","lconFirst":"TERESA","notes":"1.0","letters":"0.0","serviceAddress":"1001 W PRESIDENT GEORGE BUSH HWY, RICHARDSON, Texas,75080"},{"lconEmail":"111#111.com","hiddenCustomerID":"44762","state":"New
But my grid is not loaded with data. I get a blank grid with only headers.
Can anyone guide me through it. I have searched other questions. And tried all that mentioned in those questions(or maybe I have missed something major).
Also i have to implement server side paging. So using jsonString as datatype wont help I guess. As it then acts as local after loading. Correct me if I am wrong.
Any guidance for writing the java paging implementation would also help.
Instead of using datatype: 'jsonstring' use datatype: 'json',