text:
ALTER TABLE lead ADD COLUMN salesforce_id character varying(255);
ALTER TABLE lead ADD CONSTRAINT "lead_salesforce_id_key" UNIQUE (salesforce_id);
ALTER TABLE activity ADD COLUMN salesforce_id character varying(255);
ALTER TABLE activity ADD CONSTRAINT "activity_salesforce_id_key" UNIQUE (salesforce_id);
ALTER TABLE atype ADD COLUMN salesforce_id character varying(255);
ALTER TABLE atype ADD CONSTRAINT "atype_salesforce_id_key" UNIQUE (salesforce_id);

/*
-- Count number of conversions we ingested
select attributes->>'New Value' as conv_name,
    count(distinct lead_id) as conv_cnt
from activity
where description = 'Change Data Value'
    and value = 'Funnel Stage'
    and occurred_at >= '2016-02-01'
    and occurred_at <= '2016-02-29'
group by attributes->>'New Value'
order by attributes->>'New Value';

-- Count number of conversions we extracted
select ce.conversion_id,
    c.name,
    count(distinct ce.lead_id) as conv_cnt,
    sum(ca.credit) as credit
from conversion_event ce
join conversion c on c.id = ce.conversion_id
join campaign_attribution ca on ca.conversion_event_id = ce.id
where 1 = 1
    and occurred_at >= '2016-02-01'
    and occurred_at <= '2016-02-29'
group by ce.conversion_id, c.name
order by c.name;

--INSERT INTO activity (occurred_at, lead_id, campaign_id, description, atype_id, attributes)
SELECT conversion_event.occurred_at, conversion_event.lead_id
FROM conversion_event
LEFT OUTER JOIN (
     SELECT activity.lead_id AS lead_id, activity.occurred_at AS occurred_at, activity.campaign_id AS campaign_id
     FROM activity
     JOIN (SELECT *
        FROM campaign
        WHERE campaign.description IN ('Organic', 'New Lead', 'Open Email', 'Visit Webpage', 'Fill Out Form', 'Fill Out Form Hubspot')
     ) AS v_cpgs
        ON v_cpgs.id = activity.campaign_id
) AS v_act
   ON v_act.lead_id = conversion_event.lead_id
   AND v_act.occurred_at <= conversion_event.occurred_at
--WHERE v_act.campaign_id IS NULL
AND conversion_event.lead_id = 36
limit 1;
*/

/*
alter table "integration"
add column user_id int
    constraint integration_user_id_fkey
    references "user" (id) on delete cascade;

alter table "integration"
add column refresh_token char varying(255);

alter table "integration"
add constraint integration_unique_key
    unique (org_id, flavor);
*/

/*
update activity
set campaign_id = null
where campaign_id in
(
	select id from campaign
	where description = 'Change Data Value'
);

delete from campaign
where description = 'Change Data Value';
*/
/*

SELECT ce.id, ce.conversion_id
FROM conversion_event ce
LEFT JOIN (
select a.id, a.lead_id, a.occurred_at, a.campaign_id
from activity a
JOIN (
        SELECT campaign.id AS id, campaign.parent_id AS parent_id, campaign.level AS level, campaign.start_date AS start_date, campaign.end_date AS end_date, campaign.name AS name, campaign.display_name AS display_name, campaign.description AS description, campaign.attributes AS attributes, campaign.cnt AS cnt
        FROM campaign
        WHERE campaign.description IN (NULL, NULL, 'New Lead', 'Open Email', 'Visit Webpage', 'Fill Out Form', 'Fill Out Form Hubspot')) AS vc
        on vc.id = a.campaign_id
) va
    ON va.lead_id = ce.lead_id
    AND va.occurred_at <= ce.occurred_at
WHERE va.id IS NULL
    and ce.id in (2196, 2300)
LIMIT 15
;
*/

/*
ALTER TABLE campaign ADD COLUMN start_date TIMESTAMP WITHOUT TIME ZONE;
ALTER TABLE campaign ADD COLUMN end_date TIMESTAMP WITHOUT TIME ZONE;
ALTER TABLE campaign ADD COLUMN display_name CHARACTER VARYING(255);
ALTER TABLE campaign ADD CONSTRAINT campaign_display_name_key UNIQUE (display_name);
UPDATE campaign SET display_name = name;
*/

/*
select distinct value
from activity
where description = 'Change Data Value'
;
select distinct value,
       attributes->>'Old Value' as old_value,
       attributes->>'New Value' as new_value
from activity
where description = 'Change Data Value'
     and value = 'Lead Status'
order by old_value, new_value
;
select distinct value,
       attributes->>'Old Value' as old_value,
       attributes->>'New Value' as new_value
from activity
where description = 'Change Data Value'
     and value = 'Lead Status'
order by old_value, new_value
;
select distinct value,
       attributes->>'Old Value' as old_value,
       attributes->>'New Value' as new_value
from activity
where description = 'Change Data Value'
     and value = 'Lead Score'
order by old_value, new_value
;
select value,
       count(*) as cnt
from activity
where description = 'Change Data Value'
     and value in ('Lead Score', 'Relative Score', 'Lead Status')
group by value
;
*/
/*
;
select value,
       count(*) as cnt
from activity
where description = 'Change Data Value'
     and value in ('Funnel Stage', 'Lead Status')
group by value
*/
/*
;
select attributes->>'New Value' as new_value,
       count(*) as cnt
from activity
where description = 'Change Data Value'
     and value = 'Lead Status'
group by new_value
order by cnt desc
*/
/*
DROP TABLE lead_conversion;
DROP TABLE campaign_attribution;
DROP TABLE first_touch;
DROP TABLE conversion_path;
*/

/*
INSERT INTO conversion
VALUES (0, 0, 'beta', 'beta');
*/

--CREATE INDEX ix_activity_occurred_at ON activity (occurred_at);

/*
;
;
SELECT * FROM first_touch
ORDER BY lead_id, occurred_at
LIMIT 25;

*/

/*
-- Create new activity extract
-- Records first time a lead encounters a campaign
DROP TABLE first_touch
;
CREATE TABLE first_touch
(
  id SERIAL CONSTRAINT first_touch_id_key UNIQUE
  , occurred_at TIMESTAMP NOT NULL
  , lead_id INT NOT NULL CONSTRAINT first_touch_lead_id_fkey
      REFERENCES lead (id)
  , campaign_id INT NOT NULL CONSTRAINT first_touch_campaign_id_fkey
      REFERENCES campaign (id)
  , is_first_ever BOOLEAN
  , is_last_ever BOOLEAN
  , CONSTRAINT uq_first_touch_min_touch UNIQUE (lead_id, is_first_ever)
  , CONSTRAINT uq_first_touch_max_touch UNIQUE (lead_id, is_last_ever)
  , CONSTRAINT uq_first_touch_campaign_id UNIQUE (lead_id, campaign_id)
)
;
CREATE INDEX ix_first_touch_occurred_at
       ON first_touch (occurred_at)
;
AND a.lead_id IN
(
  99545,
  39784,
 100095,
   8054,
 105884
)
*/
/*
SELECT MIN(ft.occurred_at) as min_dt
       , MAX(ft.occurred_at) as max_dt
       , ft.lead_id
FROM first_touch ft
GROUP BY ft.lead_id
LIMIT 25;
*/
/*
SELECT ft.lead_id
       , limits.min_dt
       , limits.max_dt
       , CASE WHEN (ft.occurred_at = limits.min_dt) THEN True
       ELSE NULL
       END AS is_first_ever
       , CASE WHEN (ft.occurred_at = limits.max_dt) THEN True
       ELSE NULL
       END AS is_last_ever
FROM first_touch ft
JOIN
(
SELECT MIN(ft.occurred_at) as min_dt
       , MAX(ft.occurred_at) as max_dt
       , ft.lead_id
FROM first_touch ft
GROUP BY ft.lead_id
) limits
  ON limits.lead_id = ft.lead_id
*/
/*
;
SELECT ft.campaign_id
       , SUM(CASE WHEN ft.is_first_ever THEN 1 END) as min_cnt
       , SUM(CASE WHEN ft.is_last_ever THEN 1 END) as max_cnt
       , SUM(CASE WHEN ft.is_first_ever IS NULL AND ft.is_last_ever IS NULL THEN 1 END) as mid_cnt
       , COUNT(lead_id)
FROM first_touch ft
GROUP BY ft.campaign_id
*/
;
-- TOP 5 min_cnt
/*
22,
82,
87,
86,
15
*/
-- TOP 5 max_cnt
/*
82,
91,
93,
83,
87
*/
-- TOP 5 mid_cnt
/*
82,
90,
87,
91,
93
*/
/*
;
DROP TABLE conversion_path
;
CREATE TABLE conversion_path
(
  id SERIAL CONSTRAINT conversion_path_id_key UNIQUE
  , occurred_at TIMESTAMP NOT NULL
  , lead_id INT NOT NULL CONSTRAINT conversion_path_lead_id_fkey
      REFERENCES lead (id)
  , source_cpg_id INT NULL CONSTRAINT conversion_path_source_cpg_id_fkey
      REFERENCES campaign (id)
  , target_cpg_id INT NOT NULL CONSTRAINT conversion_path_target_cpg_id_fkey
      REFERENCES campaign (id)
  , edge_type INT
  -- Initially enforce linear paths (no trees, no DAGS, no bitters)
  -- Doubles as indexing for aggregation queries (key order matters)
  -- TODO revisit order of keys after writing queries
  , CONSTRAINT uq_conversion_path_lead_source UNIQUE (source_cpg_id, lead_id)
  , CONSTRAINT uq_conversion_path_lead_target UNIQUE (target_cpg_id, lead_id)
  , CONSTRAINT uq_conversion_path_lead_ts UNIQUE (occurred_at, lead_id)
)
;
*/
/*
;
SELECT cp.source_cpg_id
       , c.name
       , cp.target_cpg_id
       , c2.name
       , COUNT(cp.lead_id) as flow
FROM conversion_path cp
JOIN campaign c
     ON c.id = cp.source_cpg_id
JOIN campaign c2
     ON c2.id = cp.target_cpg_id
WHERE cp.edge_type = 1
      AND cp.source_cpg_id != 82
      AND cp.target_cpg_id != 82
GROUP BY cp.source_cpg_id
      , c.name
      , cp.target_cpg_id
      , c2.name
HAVING COUNT(cp.lead_id) > 2
ORDER BY flow DESC
;

SELECT cp.source_cpg_id
       , c.name
       , cp.target_cpg_id
       , c2.name
       , COUNT(cp.lead_id) as flow
FROM conversion_path cp
JOIN campaign c
     ON c.id = cp.source_cpg_id
JOIN campaign c2
     ON c2.id = cp.target_cpg_id
WHERE cp.edge_type = 3
      AND cp.source_cpg_id != 82
      AND cp.target_cpg_id != 82
GROUP BY cp.source_cpg_id
      , c.name
      , cp.target_cpg_id
      , c2.name
HAVING COUNT(cp.lead_id) > 2
ORDER BY flow DESC
LIMIT 25
;
*/

/*

WHERE cp.is_final

LIMIT 25

;
SELECT *
FROM first_touch ft
JOIN conversion_path cp
GROUP BY
*/

/*
SELECT campaign_id,
       lead_id,
       occurred_at,
       dense_rank(lead_id, occurred_at) WITHIN GROUP
           (ORDER BY lead_id DESC, occurred_at DESC) AS rnk
FROM activity
WHERE campaign_id IS NOT NULL
GROUP BY campaign_id, lead_id, occurred_at
HAVING rnk < 2
LIMIT 20;
*/

/* cool leads that converted
SELECT *
FROM lead_conversion lc
WHERE lc.lead_id IN
(
  1549,
  99545,
  39784,
 100095,
   8054,
 105884,
  68712
)

  id  | lead_id |     occurred_at
------+---------+---------------------
 2112 |   39784 | 2015-09-15 15:44:21
 2388 |  105884 | 2015-08-20 17:27:39
 2407 |  100095 | 2015-07-28 17:14:14
 2423 |   99545 | 2015-07-27 14:40:20
 4530 |    8054 | 2015-10-19 12:31:39

*/

/* quick look at potential paths
SELECT
  a.lead_id
  , a.occurred_at
  , a.campaign_id
  , c.name
  , c.description
FROM activity a
JOIN campaign c
  ON c.id = a.campaign_id
WHERE a.lead_id IN
(
  1549,
  99545,
  39784,
 100095,
   8054,
 105884,
  68712
)
ORDER BY a.lead_id, a.occurred_at
*/

/* potentially cool leads

SELECT l.id
  , COUNT(DISTINCT a.campaign_id) as cpg_cnt
FROM lead l
JOIN activity a
  ON a.lead_id = l.id
GROUP BY l.id
ORDER BY cpg_cnt DESC

   1549 |       5
  99545 |       5
  39784 |       5
 100095 |       5
   8054 |       5
 105884 |       5
  68712 |       5

*/

/*
-- for killing zombies
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
      AND state = 'idle';
*/

/*
TODO run on production after next deployment

DROP TABLE campaign_rollup;
DROP TABLE campaign_daily;
DROP TABLE score_segment;
DROP TABLE active_segment;
*/

/*
ALTER TABLE lead
ADD COLUMN hubspot_id UUID NULL CONSTRAINT lead_hubspot_id_key UNIQUE;

ALTER TABLE lead
ALTER COLUMN created_at SET DEFAULT now();

ALTER TABLE lead
ALTER COLUMN updated_at SET DEFAULT now();

ALTER TABLE activity
ADD COLUMN hubspot_id UUID NULL CONSTRAINT activity_hubspot_id_key UNIQUE;

ALTER TABLE activity
ADD COLUMN atype_id int NULL;

CREATE INDEX ix_activity_atype_id ON activity (atype_id);
*/
/*
does not have sequence
CREATE TABLE atype
(
    id int PRIMARY KEY,
    mkto_id int NULL CONSTRAINT atype_mkto_id_key UNIQUE,
    hubspot_id int NULL CONSTRAINT atype_hubspot_id_key UNIQUE,
    name text NOT NULL DEFAULT '',
    description text NOT NULL DEFAULT ''
)
*/

--TRUNCATE TABLE temp_cpg_trend_domain_;
/*
SELECT c.id,
       c.name,
       l.id,
       l.email,
       l.description,
       l.mkto_id,
       l.valid,
       l.complete,
       a.rnk,
       a.occurred_at
FROM campaign AS c
JOIN
(
SELECT campaign_id,
       lead_id,
       occurred_at,
       dense_rank(1, 2) OVER (PARTITION BY campaign_id
              ORDER BY lead_id DESC, occurred_at DESC) AS rnk
FROM activity
WHERE campaign_id IS NOT NULL
) AS a
    ON a.campaign_id = c.id
        AND a.rnk < 5
JOIN lead AS l
    ON l.id = a.lead_id
LIMIT 20;

SELECT campaign_id,
       lead_id,
       occurred_at,
       dense_rank(lead_id, occurred_at) WITHIN GROUP
           (ORDER BY lead_id DESC, occurred_at DESC) AS rnk
FROM activity
WHERE campaign_id IS NOT NULL
GROUP BY campaign_id, lead_id, occurred_at
HAVING rnk < 2
LIMIT 20;

SELECT campaign_id,
    lead_id,
    occurred_at,
    rank() OVER (PARTITION BY campaign_id
                 ORDER BY lead_id DESC, occurred_at DESC) AS rnk
FROM activity
WHERE campaign_id IS NOT NULL
LIMIT 20;

EXPLAIN ANALYZE
SELECT *
FROM lead
LIMIT 10;
*/


text:
<?php

namespace BingAds\Reporting;

/**
 * Defines the data columns that you can include in an age and gender demographic report.
 * @link http://msdn.microsoft.com/en-us/library/bb671786(v=msads.90).aspx AgeGenderDemographicReportColumn Value Set
 */
final class AgeGenderDemographicReportColumn
{
    /** The report will include a column that contains the account name. */
    const AccountName = 'AccountName';

    /** The report will include a column that contains the account number. */
    const AccountNumber = 'AccountNumber';

    /** The report will include a column that contains the time period of each report row. */
    const TimePeriod = 'TimePeriod';

    /** The report will include a column that contains the campaign name. */
    const CampaignName = 'CampaignName';

    /** The report will include a column that contains the ad group name. */
    const AdGroupName = 'AdGroupName';

    /** The report will include a column that contains the ad distribution medium. For possible values, see the AdDistributionReportFilter value set. */
    const AdDistribution = 'AdDistribution';

    /** The report will include a column that contains the age group of users that could click the ad. For possible values, see the AgeGroupReportFilter value set. */
    const AgeGroup = 'AgeGroup';

    /** The report will include a column that contains the gender of users that could click the ad. For possible values, see the GenderReportFilter value set. */
    const Gender = 'Gender';

    /** The report will include a column that contains the estimated number of times that an ad could be served to a particular age group or gender. */
    const EstimatedImpressionPercent = 'EstimatedImpressionPercent';

    /** The report will include a column that contains the estimated number of times that an ad will be clicked by a particular age group or gender. */
    const EstimatedClickPercent = 'EstimatedClickPercent';

    /** The report will include a column that contains the estimated click-through rate as a percentage. The formula for calculating the estimated click-through rate is (estimated clicks / estimated impressions) * 100. */
    const EstimatedCtr = 'EstimatedCtr';

    /** The report will include a column that contains the language used by the website that displayed the ad. */
    const Language = 'Language';
}

redpajama_set_name: RedPajamaGithub


text:
using namespace kinect_device;
using namespace oni::driver;
#define DEFAULT_FPS 30
KinectDevice::KinectDevice(INuiSensor * pNuiSensor):m_pDepthStream(NULL), m_pColorStream(NULL),m_pNuiSensor(pNuiSensor)
{
	m_numSensors = 3;

	m_sensors[0].pSupportedVideoModes = XN_NEW_ARR(OniVideoMode, 3);
	m_sensors[0].sensorType = ONI_SENSOR_DEPTH;
	m_sensors[0].numSupportedVideoModes = 3;
	m_sensors[0].pSupportedVideoModes[0].pixelFormat = ONI_PIXEL_FORMAT_DEPTH_1_MM;
	m_sensors[0].pSupportedVideoModes[0].fps = DEFAULT_FPS;
	m_sensors[0].pSupportedVideoModes[0].resolutionX = 640;
	m_sensors[0].pSupportedVideoModes[0].resolutionY = 480;

	m_sensors[0].pSupportedVideoModes[1].pixelFormat = ONI_PIXEL_FORMAT_DEPTH_1_MM;
	m_sensors[0].pSupportedVideoModes[1].fps = DEFAULT_FPS;
	m_sensors[0].pSupportedVideoModes[1].resolutionX = 320;
	m_sensors[0].pSupportedVideoModes[1].resolutionY = 240;

	m_sensors[0].pSupportedVideoModes[2].pixelFormat = ONI_PIXEL_FORMAT_DEPTH_1_MM;
	m_sensors[0].pSupportedVideoModes[2].fps = DEFAULT_FPS;
	m_sensors[0].pSupportedVideoModes[2].resolutionX = 80;
	m_sensors[0].pSupportedVideoModes[2].resolutionY = 60;

	m_sensors[1].pSupportedVideoModes = XN_NEW_ARR(OniVideoMode, 3);
	m_sensors[1].sensorType = ONI_SENSOR_COLOR;
	m_sensors[1].numSupportedVideoModes = 3;
	m_sensors[1].pSupportedVideoModes[0].pixelFormat = ONI_PIXEL_FORMAT_RGB888;
	m_sensors[1].pSupportedVideoModes[0].fps         = 12;
	m_sensors[1].pSupportedVideoModes[0].resolutionX = 1280;
	m_sensors[1].pSupportedVideoModes[0].resolutionY = 960;

	m_sensors[1].pSupportedVideoModes[1].pixelFormat = ONI_PIXEL_FORMAT_RGB888;
	m_sensors[1].pSupportedVideoModes[1].fps = DEFAULT_FPS;
	m_sensors[1].pSupportedVideoModes[1].resolutionX = 640;
	m_sensors[1].pSupportedVideoModes[1].resolutionY = 480;

	m_sensors[1].pSupportedVideoModes[2].pixelFormat = ONI_PIXEL_FORMAT_YUV422;
	m_sensors[1].pSupportedVideoModes[2].fps         = 15;
	m_sensors[1].pSupportedVideoModes[2].resolutionX = 640;
	m_sensors[1].pSupportedVideoModes[2].resolutionY = 480;

	m_sensors[2].pSupportedVideoModes = XN_NEW_ARR(OniVideoMode, 3);
	m_sensors[2].sensorType = ONI_SENSOR_IR;
	m_sensors[2].numSupportedVideoModes = 3;
	m_sensors[2].pSupportedVideoModes[0].pixelFormat = ONI_PIXEL_FORMAT_RGB888;
	m_sensors[2].pSupportedVideoModes[0].fps = DEFAULT_FPS;
	m_sensors[2].pSupportedVideoModes[0].resolutionX = 640;
	m_sensors[2].pSupportedVideoModes[0].resolutionY = 480;

	m_sensors[2].pSupportedVideoModes[1].pixelFormat = ONI_PIXEL_FORMAT_GRAY16;
	m_sensors[2].pSupportedVideoModes[1].fps = DEFAULT_FPS;
	m_sensors[2].pSupportedVideoModes[1].resolutionX = 640;
	m_sensors[2].pSupportedVideoModes[1].resolutionY = 480;

	m_sensors[2].pSupportedVideoModes[2].pixelFormat = ONI_PIXEL_FORMAT_GRAY8;
	m_sensors[2].pSupportedVideoModes[2].fps = DEFAULT_FPS;
	m_sensors[2].pSupportedVideoModes[2].resolutionX = 640;
	m_sensors[2].pSupportedVideoModes[2].resolutionY = 480;
}

KinectDevice::~KinectDevice()
{
	if (m_pNuiSensor)
		m_pNuiSensor->NuiShutdown();

	if (m_pDepthStream != NULL)
		XN_DELETE(m_pDepthStream);

	if (m_pColorStream!= NULL)
		XN_DELETE(m_pColorStream);

	if (m_pNuiSensor)
		m_pNuiSensor->Release();
}

OniStatus KinectDevice::getSensorInfoList(OniSensorInfo** pSensors, int* numSensors)
{
	*numSensors = m_numSensors;
	*pSensors = m_sensors;
	return ONI_STATUS_OK;
}

StreamBase* KinectDevice::createStream(OniSensorType sensorType)
{
	BaseKinectStream* pImage = NULL;

	if (sensorType ==  ONI_SENSOR_COLOR )
	{
		if (m_pColorStream == NULL)
		{
			m_pColorStream = XN_NEW(KinectStreamImpl, m_pNuiSensor, sensorType);

		}
		pImage = XN_NEW(ColorKinectStream, m_pColorStream);
	}
	else if (sensorType == ONI_SENSOR_DEPTH )
	{
		if (m_pDepthStream == NULL)
		{
			m_pDepthStream = XN_NEW(KinectStreamImpl, m_pNuiSensor, sensorType);
		}
		pImage = XN_NEW(DepthKinectStream, m_pDepthStream);
	}
	if (sensorType ==  ONI_SENSOR_IR )
	{
		if (m_pColorStream == NULL)
		{
			m_pColorStream = XN_NEW(KinectStreamImpl, m_pNuiSensor, sensorType);
		}
		pImage = XN_NEW(IRKinectStream, m_pColorStream);

	}
	return pImage;
}

void kinect_device::KinectDevice::destroyStream(oni::driver::StreamBase* pStream)
{
	XN_DELETE(pStream);
}

OniStatus KinectDevice::setProperty(int propertyId, const void* data, int dataSize)
{
	switch (propertyId) {
	case ONI_DEVICE_PROPERTY_IMAGE_REGISTRATION:
		{
			EXACT_PROP_SIZE_OR_RETURN(dataSize, OniImageRegistrationMode);
			if (m_pDepthStream) {
				OniImageRegistrationMode* pMode = (OniImageRegistrationMode*)data;
				m_pDepthStream->setImageRegistrationMode(*pMode);
				return ONI_STATUS_OK;
			}
			else
			{
				return ONI_STATUS_ERROR;
			}
		}
	case KINECT_DEVICE_PROPERTY_CAMERA_ELEVATION:
		{
			EXACT_PROP_SIZE_OR_RETURN(dataSize, long);
			return SUCCEEDED(m_pNuiSensor->NuiCameraElevationSetAngle(*(long*)data)) ? ONI_STATUS_OK : ONI_STATUS_ERROR;
		}
	case KINECT_DEVICE_PROPERTY_EMITTER_STATE:
		{
			EXACT_PROP_SIZE_OR_RETURN(dataSize, OniBool);
			OniBool b = *(OniBool*)data;
			return SUCCEEDED(m_pNuiSensor->NuiSetForceInfraredEmitterOff(!b)) ? ONI_STATUS_OK : ONI_STATUS_ERROR;
		}
	}

	return ONI_STATUS_NOT_IMPLEMENTED;
}

OniStatus KinectDevice::getProperty(int propertyId, void* data, int* pDataSize)
{
	switch (propertyId) {
	case ONI_DEVICE_PROPERTY_IMAGE_REGISTRATION:
		{
			EXACT_PROP_SIZE_OR_RETURN(*pDataSize, OniImageRegistrationMode);
			if (m_pDepthStream)
			{
				OniImageRegistrationMode* pMode = (OniImageRegistrationMode*)data;
				*pMode = m_pDepthStream->getImageRegistrationMode();
				return ONI_STATUS_OK;
			}
			else
			{
				return ONI_STATUS_ERROR;
			}
		}
	case KINECT_DEVICE_PROPERTY_CAMERA_ELEVATION:
		{
			EXACT_PROP_SIZE_OR_RETURN(*pDataSize, long);
			return SUCCEEDED(m_pNuiSensor->NuiCameraElevationGetAngle((long*)data)) ? ONI_STATUS_OK : ONI_STATUS_ERROR;
		}
	case KINECT_DEVICE_PROPERTY_ACCELEROMETER:
		{
			EXACT_PROP_SIZE_OR_RETURN(*pDataSize, KinectVector3f);
			Vector4 v;
			if (SUCCEEDED(m_pNuiSensor->NuiAccelerometerGetCurrentReading(&v)))
			{
				KinectVector3f* pVector3f = (KinectVector3f*)(data);
				pVector3f->x = v.x;
				pVector3f->y = v.y;
				pVector3f->z = v.z;
				return ONI_STATUS_OK;
			}
			else
			{
				return ONI_STATUS_ERROR;
			}
		}
	case KINECT_DEVICE_PROPERTY_EMITTER_STATE:
		{
			EXACT_PROP_SIZE_OR_RETURN(*pDataSize, OniBool);
			*(OniBool*)data = !m_pNuiSensor->NuiGetForceInfraredEmitterOff();
			return ONI_STATUS_OK;
		}
	case KINECT_DEVICE_PROPERTY_AUDIO_ARRAY_ID:
		{
			char* arrayId = (char*)data;
			BSTR wcsArrayId = m_pNuiSensor->NuiAudioArrayId();
			size_t len = wcstombs(arrayId, wcsArrayId, *pDataSize - 1);
			SysFreeString(wcsArrayId);

			if (len == -1)
			{
				*pDataSize = 0;
				return ONI_STATUS_ERROR;
			}
			else
			{
				arrayId[len] = '\0';
				*pDataSize = int(len + 1);
				return ONI_STATUS_OK;
			}
		}
	}

	return ONI_STATUS_NOT_IMPLEMENTED;
}

OniBool KinectDevice::isPropertySupported(int propertyId)
{
	switch (propertyId)
	{
	case ONI_DEVICE_PROPERTY_IMAGE_REGISTRATION:
	case KINECT_DEVICE_PROPERTY_CAMERA_ELEVATION:
	case KINECT_DEVICE_PROPERTY_ACCELEROMETER:
	case KINECT_DEVICE_PROPERTY_EMITTER_STATE:
	case KINECT_DEVICE_PROPERTY_AUDIO_ARRAY_ID:
		return true;
	}
	return false;
}

OniBool KinectDevice::isCommandSupported(int commandId)
{
	return ONI_STATUS_NOT_IMPLEMENTED;
}

OniStatus KinectDevice::tryManualTrigger()
{
	return ONI_STATUS_NOT_IMPLEMENTED;
}

OniBool KinectDevice::isImageRegistrationModeSupported(OniImageRegistrationMode mode)
{
	return (mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR || mode == ONI_IMAGE_REGISTRATION_OFF);
}


text:
'use strict';

(function () {
   angular
      .module('VotingApp', ['ngResource'])
      .controller('indexController', ['$scope', '$resource', function ($scope, $resource) {

         /***** INITIALIZE *****/
         $scope.loader = { isLoadingPolls: true };
         $scope.polls = [];

         var User = $resource('/api/user');
         var Polls = $resource('/api/polls');

         /***** CONTROLLER FUNCTIONS *****/
         function getUser () {
            User.get(function (res) {
               if (res.displayName !== null) {
                  $("#authorized-navbar").removeClass("hide");
                  $("#unauthorized-navbar").addClass("hide");
               }
            }, function (err) {
               $("#authorized-navbar").addClass("hide");
               $("#unauthorized-navbar").removeClass("hide");
            });
         };

         function getPolls () {
            Polls.get(function (res) {
               $scope.loader.isLoadingPolls = false;
               $scope.polls = res.data;
               $('.all-polls table').removeClass('hidden');
            }, function (err) {
               $scope.loader.isLoadingPolls = false;

               console.log('getPolls error', err);
               alert('Oops! Something went wrong. Try again later.');
            });
         }

         getUser();
         getPolls();
      }]);
})();


text:
module M2TSParser
  module Appendix
    class ARIBString < BinaryParser::TemplateBase
      require 'tsparser'
      def content_description
        if to_s
          begin
            arib_str = TSparser::AribString.new(TSparser::Binary.new(to_s))
            return arib_str.to_utf_8
          rescue
            return "error"
          end
        else
          return ""
        end
      end
    end
  end
end


text:
//
//  DirectoryViewController.m
//  7sEvents
//
//  Created by 7Seconds Technologies on 16/06/14.
//  Copyright (c) 2014 7Seconds Technologies. All rights reserved.
//

#import "PortalViewController.h"
#import "MenuViewController.h"

@interface PortalViewController (){

    IBOutlet UILabel *titleLabel;
    UIImageView *blurImageView;
    CGImageRef cgImg;
    CIContext *context;
}

@end

@implementation PortalViewController

@synthesize portalView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.screenName = @"Portal";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *label = [[MenuViewController menuController] labelWithTitle:@"Portal"];
    [label setTextColor:[UIColor darkGrayColor]];
    self.navigationItem.titleView = label;

    UIImage *rightImage = [UIImage imageNamed:@"portalMenuButton.png"];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:nil action:nil];
    [[self navigationItem] setRightBarButtonItem:rightButton];

    NSString *fullURL = @"http://portaltransparencia.gob.mx/buscador/search/search.do?method=begin";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [portalView loadRequest:requestObj];
    portalView.scalesPageToFit = YES;
    portalView.delegate = self;

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.view viewWithTag:25].hidden = YES;
    [titleLabel setText:@"Conóce la SFP"];
}

-(UIImageView *)loadBlurView{

    //**BLUR**//
    //Get a screen capture from the current view.
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the image
    CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];

    CGAffineTransform transform = CGAffineTransformIdentity;
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    [clampFilter setValue:blurImg forKey:@"inputImage"];
    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    [gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat:7.0f] forKey:@"inputRadius"];

    context = [CIContext contextWithOptions:nil];
    cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]];
    UIImage *outputImg = [UIImage imageWithCGImage:cgImg];

    //Add UIImageView to current view.
    UIView *blurView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [blurView setBackgroundColor:[UIColor colorWithWhite:0.05 alpha:0.5]];
    UIImageView *imagView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imagView.image = outputImg;
    [imagView addSubview:blurView];
    return imagView;
}

- (IBAction)showMenu:(id)sender
{
    blurImageView = [self loadBlurView];
    [blurImageView setAlpha:0];
    [self.view addSubview:blurImageView];
    [UIView animateWithDuration:0.4 animations:^{
        [blurImageView setAlpha:1];
    }];
    [[MenuViewController menuController] showMenu];
}

-(void)hideMenuController{
    [blurImageView setAlpha:1];
    [UIView animateWithDuration:0.4 animations:^{
        [blurImageView setAlpha:0];
    } completion:^(BOOL fin){
        [blurImageView removeFromSuperview];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


text:
public class TesteJanela {

	public static void main(String[] args) {
		Janela window = new Janela();
		window.altura = 200;
		window.largura = 300;

		Janela window2 = new Janela();
		window2.altura = 100;
		window2.largura = 50;

		System.out.println(window.calculaArea());
		System.out.println(window2.calculaArea());

	}

}


text:
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import compose from 'lodash/fp/compose';
import {combineLatest, of as of$} from 'rxjs';
import {map, switchMap} from 'rxjs/operators';

import {queryChannelsById} from '@queries/servers/channel';
import {observeLicense, observeConfigBooleanValue} from '@queries/servers/system';
import {observeCurrentUser} from '@queries/servers/user';
import {getTimezone} from '@utils/user';

import Results from './results';

import type {WithDatabaseArgs} from '@typings/database/database';

type enhancedProps = WithDatabaseArgs & {
    fileChannelIds: string[];
}

const enhance = withObservables(['fileChannelIds'], ({database, fileChannelIds}: enhancedProps) => {
    const fileChannels = queryChannelsById(database, fileChannelIds).observeWithColumns(['displayName']);
    const currentUser = observeCurrentUser(database);

    const enableMobileFileDownload = observeConfigBooleanValue(database, 'EnableMobileFileDownload');

    const complianceDisabled = observeLicense(database).pipe(
        switchMap((lcs) => of$(lcs?.IsLicensed === 'false' || lcs?.Compliance === 'false')),
    );

    const canDownloadFiles = combineLatest([enableMobileFileDownload, complianceDisabled]).pipe(
        map(([download, compliance]) => compliance || download),
    );

    return {
        currentTimezone: currentUser.pipe((switchMap((user) => of$(getTimezone(user?.timezone))))),
        isTimezoneEnabled: observeConfigBooleanValue(database, 'ExperimentalTimezone'),
        fileChannels,
        canDownloadFiles,
        publicLinkEnabled: observeConfigBooleanValue(database, 'EnablePublicLink'),
    };
});

export default compose(
    withDatabase,
    enhance,
)(Results);


text:
package fr.univnantes.lina.uima.tkregex.model.automata;

import org.apache.uima.cas.text.AnnotationFS;

public class StateExploration {
	private TransitionIterator iterator;
	private AnnotationFS annotation;
	private Transition transition;
	StateExploration(TransitionIterator iterator,
					 AnnotationFS annotation, Transition transition) {
		super();
		this.iterator = iterator;
		this.annotation = annotation;
		this.transition = transition;
	}

	public AnnotationFS getAnnotation() {
		return annotation;
	}

	public TransitionIterator getIterator() {
		return iterator;
	}

	public Transition getTransition() {
		return transition;
	}

	public StateExploration doClone() {
		return new StateExploration(iterator.doClone(), annotation, transition);
	}
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WifiSnifferPositioningService.radiomap;

namespace WifiSnifferPositioningService.location.wifi
{
	public class BCS
	{
		private static double defaultErrorDistance = 20;

		//The number of elements used in the 'BestCandidateSet'
		private int maxSize;

		//list of estimates sorted according to their corresponding probabilities
		private SortedDictionary<double, Vertex> locations = new SortedDictionary<double, Vertex>();

		public BCS()
		{
			maxSize = 3;
		}

		public BCS(int maxSize)
		{
			this.maxSize = maxSize;
		}

		public void clear()
		{
			locations = new SortedDictionary<double, Vertex>();
		}

		public void setMaxSize(int size)
		{
			this.maxSize = size;
		}

		public int getMaxSize()
		{
			return maxSize;
		}

		/**
		 * Add a new element to the 'best candidate set' - if it qualifies.
		 * I.e., the distance is lower than the current worst candidate
		 * NOTE: Currently, this is hardcoded to distance, i.e., to be used with NNS
		 * @param v Vertex to be inserted if the distance parameter is lower than the current worst element in the BCS.
		 * @param distance The (NNS) distance of the vertex which determines whether v will be inserted in the BCS.
		 */
		public bool add(Vertex v, double distance)
		{
			if (locations.Count >= maxSize && distance > locations.Keys.Last())
			{
				return false; //is not better than the worst element
			}
			else //insert order: From best (lowest NNS distance) to worst (highest NNS distance).
			{
				//'hack' in case of duplicate values
				while (locations.ContainsKey(distance))
					distance += 0.0001;

				//Impl. note: Check for emptiness first as lastKey() will throw NoSuchElement if empty.
				//if (locations.isEmpty() || distance < locations.lastKey())
				locations.Add(distance, v);

				if (locations.Count > maxSize)
					locations.Remove(locations.Last().Key);

			}
			return true;
		}

		/**
		 * Returns the list of the best estimates and their associated probablities in sorted order.
		 * @return
		 */
		public SortedDictionary<Double, Vertex> getAll()
		{
			return locations;
		}

		/**
		 * Returns the probabilityGeoPosition with the highest probability in the BestEstimates3
		 * @return
		 */
		public Vertex getFirst()
		{
			return locations.First().Value; //.get(locations.firstKey());
		}

		/**
		 * Returns the probabilityGeoPosition with the lowest probability in the BestEstimates3
		 * @return
		 */
		public Vertex getLast()
		{
			return locations.Last().Value; //.get(locations.lastKey());
		}

		/**
		 * Returns the vertex with the nth-highest probability in the BestEstimates (the best having n=1)
		 * @param n
		 * @return
		 */
		public Vertex getNthHighest(int n)
	{
		int i = 1; //we start at 1 (rather than zero)
		foreach (Vertex v in locations.Values)
		{
			if (i == n)
				return v;
			i++;
		}
		return null;
	}

		/**
		 * Returns the distance from the best (returned) estimate to the n-1 remaining estimates in the BCS
		 */
		public double getAvgDist()
	{
		//average distance from best estimate
		double avgDist = 0.0;
		if (locations.Count > 1)
		{
			Vertex best = locations.First().Value;
			AbsoluteLocation bestLoc, curLoc;
			bestLoc = best.AbsoluteLocations.First();

			foreach (Vertex v in locations.Values)
			{
				if (best == v)
					continue;

				curLoc = v.AbsoluteLocations.First();
				avgDist += baselogic.DistanceMeasurements.CalculateMoveddistanceInMeters(
					(double)bestLoc.latitude.Value, (double)bestLoc.longitude.Value,
					(double)curLoc.latitude.Value, (double)curLoc.longitude.Value);
			}
			avgDist /= (locations.Count - 1);
		}
		else
			avgDist = defaultErrorDistance; //default value

		return avgDist;
		//AccuracyGuarantees.timeDistanceSmoothing(avgDist);
	}

		public double getAvgDistanceToNthHighest(int n)
	{
		//average distance from best estimate
		double avgDist = 0.0;
		if (n > 1)
		{
			Vertex best = locations.First().Value; //.get(locations.firstKey());
			AbsoluteLocation bestLoc, curLoc;
			int i = 1;

			//NOTE: Are values returned in sorted order?
			foreach (Vertex v in locations.Values)
			{
				if (best == v)
					continue;

				if (i++ == n)
					break;

				bestLoc = best.AbsoluteLocations.First();;
				curLoc = v.AbsoluteLocations.First();
				avgDist += baselogic.DistanceMeasurements.CalculateMoveddistanceInMeters(
					(double)bestLoc.latitude.Value, (double)bestLoc.longitude.Value,
					(double)curLoc.latitude.Value, (double)curLoc.longitude.Value);
			}
			avgDist /= n - 1;
		}
		else
			avgDist = defaultErrorDistance; //default value

		return avgDist;
		//AccuracyGuarantees.timeDistanceSmoothing(avgDist);
	}

		public double getDistanceToNthHighest(int n)
		{
			Vertex best = getFirst();
			Vertex v = getNthHighest(n);
			if (best != null && v != null)
			{
				AbsoluteLocation bestLoc, nLoc;
				bestLoc = best.AbsoluteLocations.First();
				nLoc = v.AbsoluteLocations.First();
				return baselogic.DistanceMeasurements.CalculateMoveddistanceInMeters(
					 (double)bestLoc.latitude.Value, (double)bestLoc.longitude.Value,
					 (double)nLoc.latitude.Value, (double)nLoc.longitude.Value);
			}

			return defaultErrorDistance; //default
		}

		public double getMaxDistance()
	{
		//return getDistanceToNthHighest(getMaxSize());
		try
		{
			Vertex best = getFirst();
			if (best == null)
				return defaultErrorDistance;

			double maxDistance = 0, curDistance = 0;
			AbsoluteLocation bestLoc, curLoc;
			bestLoc = best.AbsoluteLocations.First();

			foreach (Vertex v in locations.Values)
			{
				//skip the best
				if (v == best)
					continue;

				//Calculate the distance from the best location to the current location
				curLoc = v.AbsoluteLocations.First();
				curDistance = baselogic.DistanceMeasurements.CalculateMoveddistanceInMeters(
								(double)bestLoc.latitude.Value, (double)bestLoc.longitude.Value,
								(double)curLoc.latitude.Value, (double)curLoc.longitude.Value);

				//Is this the worst distance yet?
				if (curDistance > maxDistance)
				{
					maxDistance = curDistance;
				}
			}

			return maxDistance; //Return the worst distance
		}
		//HACK: CATCHING GENERIC EXCEPTION
		//prompted by NoSuchElementException - but wasn't able to reproduce the error
		catch (Exception)
		{
			return defaultErrorDistance;
		}
	}

		private static List<Double> smoothingQueue;
		/**
		 * This one simply averages the region sizes.
		 * 	 - the same idea was used in "Location Sensing and Privacy in a Context-Aware Computing Environment" by Smailagic et al.  for smoothing
		 * position estimates while tracking
		 * - The method can easily be made more elegant... probably better off as independent class
		 * - history length is currently fixed to 20.
		 *
		 * @param p
		 * @param regions
		 * @param estimatedRegion
		 * @return
		 */
		public static double timeDistanceSmoothing(double estimatedRegion)
	{
		if (smoothingQueue == null)
		{
			smoothingQueue = new List<Double>();
		}
		smoothingQueue.Add(estimatedRegion);
		if (smoothingQueue.Count > 20)
			smoothingQueue.Remove(19);

		double total = 0;
		foreach (double d in smoothingQueue)
		{
			total += d;
		}
		return total / smoothingQueue.Count;
	}
	}
}


text:
![RoyalScriptLogo](https://github.com/jweinst1/Royalscript/blob/gh-pages/images/crown-png-4.png)

[Playground](https://jweinst1.github.io/Royalscript/playground)

[REPL](https://jweinst1.github.io/Royalscript/repl)

[Website](https://jweinst1.github.io/Royalscript/)

[NPM](https://www.npmjs.com/package/royalscript)

## Intro

RoyalScript is a functional programming language that aims to combine the utility of popular functional programming languages with the mutable data structures found in object oriented languages. The language is also quickly compiled to compressed JavaScript, so it can be run in the browser, without having to download anything on your computer. Lastly, RoyalScript is also inter-operable with JavaScript functions. You can call native JavaScript functions from RoyalScript as well!

RoyalScript is a both a compiled or interpreted language, this depends on the JavaScript environment that RoyalScript is run in.

## Contents

- [Installation and Setup](#installation-and-setup)
- [Computational Functions](#computational-functions)
- [Syntax](#syntax)
- [Types](#types)
    - [Literal](#literal)
    - [Primitive Types](#primitive-types)
    - [Struct Type](#struct-type)
- [Comments](#comments)
- [Arithmetic Functions](#arithmetic-functions)
    - [Nested Expressions](#nested-expressions)
- [Logical Functions](#logical-functions)
- [Variables](#variables)
- [Grouping Functions](#grouping-functions)
- [Printing](#printing)
- [String Functions](#string-functions)
- [List Functions](#list-functions)
- [Conditional Functions](#conditional-functions)
- Procs(1 or 2 argument functions)
- [Loop Functions](#loop-functions)
- [List Construction and Comprehension](#list-construction-and-comprehension)
- [Defining Functions](#defining-functions)
- [Structs](#structs)

## Installation and Setup

To install RoyalScript, you can use npm by typing the command:

```
$ npm install -g royalscript
```
When run natively, RoyalScript uses `.royal` files. To compile a RoyalScrpt file to JavaScript, use

```
royalscript -j path/to/royalscriptfile
```
This will output the transcompiled, compressed JavaScript in your terminal. To compile and run a royalscript file, you can use

```
royalscript -e path/to/royalscriptfile
```

You can also use the `Main.Compile` function in your node programs.

## Computational Functions

The major difference between RoyalScript and other functional languages like Scheme, is that RoyalScript functions do not *always* evaluate to some resulting value. They do not always return a value. Some functions, such as the `for` function, loops over some list of values. Another example is the (Missing).

RoyalScript is meant to offer more options for computation than just recursion, although recursion is definitely usable.

In fact, every RoyalScript program must have at least one or more functions, otherwise you will raise an error.

## Syntax

In RoyalScript, all statements abide by a concise function syntax as follows

```
<FUNCTION>(<ARGUMENT 1>, <ARGUMENT 2>....)

nested functions:

<FUNCTION 1>(<FUNCTION 2>(<ARGUMENT 1>), <ARGUMENT 1>....)

series of functions:

<FUNCTION>(<ARGUMENT 1>, <ARGUMENT 2>...), <FUNCTION>()
```

So statements or programs in RoyalScript consist purely of functions and their arguments.

## Types

RoyalScript is a dynamically typed language. This means that variables do not have to be declared with a specific type, nor do functions need specified paramater or return types.

However, one of the goals of RoyalScript is to provide a stronger type system than JavaScript or other functional languages with the type and is functions, and the struct system RoyalScript has.

### Literal

RoyalScript variables, function names, and struct names, or any name is represented as a literal. A literal is pure representation of data in UTF-8 format. For example,

```
=(a, 4)
```
This function binds the literal `a` to `4`. Using `a` in any function call without binding it previously will throw an error.

### Primitive Types

RoyalScript has 5 primitive types

* Number
* String
* Boolean
* List(Array)
* Function

Additionally, the null type exists to represent a *none* or *void* type present in many other languages.

Strings in RoyalScript are implemented with backtick barriers, `` ` ``, as opposed to quotes `"` with most languages. This is primarily due to the optimized method of parsing RoyalScript, which will be explained in another section.

### Struct Type

RoyalScript structs, grouped fields of data, create a new type for every different struct that is named and declared. These can be checked using the `type()` and `is()` functions, which are covered in more detail later.

## Comments

Comments are the only feature of RoyalScript that are not functions or function calls. They are always denoted by semicolons, `;`, and can extend multiple lines or even be in the middle of code.

```
>> +(1, 2) ;this is a comment;
3
>> +(1, ;you can put them in the middle of code too; 2)
3
```

## Arithmetic Functions

RoyalScript has several arithmetic functions that are used to perform such operations on numbers. RoyalScript numbers are identical to JavaScript numbers which represent both integers and floats.

### `+(...)`

The `+()` function adds an arbitrary number of arguments together. These can be numbers, or strings, which will be concatenated instead.

```
>> +(4, 4)
>> 8
>> +(1, 3, 2)
>> 6
>> +(`hello`, ` `, `world!`)
>> "hello world!"
```

RoyalScript does not allow trailing commas in function calls:

```
>> +(8, 7, )
>> SyntaxError
```

RoyalScript will coerce a number to a string type if they are both calld by the `+()` function.

```
>> +(100, `k`)
>> "100k"
```

### `-(...)`

The `-()` function subtracts an arbitrary number of integers. It is associative to the left most argument, meaning:

```
>> -(1, 1, 1)
>> -1
>> -(1, -1)
>> 2
```

### `\*(...)`

The `*()` function multiplies an arbitrary number of integers. It is associative to the left most argument, meaning:

```
>> *(1, -1, 1)
>> -1
>> *(10, 2)
>> 20
```
### `/(...)`

The `/()` function divides an arbitrary number of integers. It is left associative.

```
>> /(3, 5)
>> 0.6
>> /(3, 10)
>> 0.3
>> /(3, 11)
>> 0.2727272727272727
```

### `//(...)`

The `//()` function uses floor divsion, by rounding the result to the next lowest integer, on an arbitrary amount of numbers. It is left associative

```
>> //(1, 2)
>> 0
>> //(1, 10)
>> 0
>> //(55, 10)
>> 5

```

### `%(...)`

The `%()` function takes the remainder of an arbitrary amount of numbers. It is also left associative.

```
%(8, 3)
2
%(8, 1)
0
%(44, 8)
4
```
### `\*\*(...)`

The `**()` function, called the exponent function, raises some left hand number to the power of the right hand number.

```
**(2, 2)
4
```

### Nested Expressions

Arithmetic functions, like all functions in RoyalScript are nestable, you can call functions within functions to make for more powerful computability. See these examples:

```
>> +(4, -(2, 3), *(8, 8))
67
>> -(3,
..    +(3, 2)
.. )
2
>> **(2, %(3, 3))
1
```

As demonstrated, any function, such as the `-()` can stretch across multiple lines if you so chose to do so.

## Logical Functions

RoyalScript defines a series of logical functions for comparing numbers, strings, and boolans. RoyalScript also provides functions to compare collections list lists or specific types like structs.

Unlike the arithmetic functions, some of these functions only take 2 and only 2 arguments, otherwise throwing an error.

### `==(arg1, arg2)`

The `==()` function checks if two arguments are of the same type and same value, except for lists or structs, which will not properly compare using this function.

```
==(1, 1)
true
==(2, 1)
false
==(2, `2`)
false
==(2, +(2, 0))
true
```

### `!=(arg1, arg2)`

The `!=()` works exactly the same yet checks if two arguments are unequal to each other, or not the same type.

```
!=(1, 1)
false
!=(3, 3, 3)
Argument Error: Got improper arguments but expected 2.
!=(false, true)
true
```
### `>(arg1, arg2)`

The `>()` function, also called the greater than function compares two numbers and returns true if the left hand argument is greater than the right hand argument.

### `<(arg1, arg2)`

The `<()` function, also called the less than function compares two numbers and returns true if the left hand argument is less than the right hand argument.

### `<=(arg1, arg2)`

The `<=()` function, also called the less than or qual to  function compares two numbers and returns true if the left hand argument is less than or equal to the right hand argument.

### `>=(arg1, arg2)`

The `>=()` function, also called the less than or qual to  function compares two numbers and returns true if the left hand argument is less than or equal to the right hand argument.

Here are some examples:

```
>> <(3, 5)
true
>> <(3, **(2, 6))
true
>> ==(
..  ==(1, 3), >(200, 4)
.. )
false
```

### `||(...)`

The `||()` function is the logical OR function. It takes an arbitrary number of expressions that evaluate to booleans or boolean values themselves. If any of the arguments evaluates to true, the function will evaluate to true.

```
>> ||(==(4, 3), 5)
5
>> ||(==(4, 3), true)
true
>> ||(==(4, 3), false)
false
```

### `&&(...)`

The `&&()` function is the logical AND function. It takes an arbitrary number of expressions that evaluate to booleans or boolean values themselves. If all of the arguments evaluates to true, the function will evaluate to true. If one is false, it evaluates to false.

```
>> &&(true, false, true)
false
>> &&(==(9, 9), true, true)
true
>> &&(
..   ||(==(3, 4), <=(1, 5)), true
.. )
true
```

### `not(arg1)`

The `not()` function is the logical NOT function. It takes in an expression that evaluates to a boolean or a boolean itself and returns the opposite boolean to that argument

```
>> not(8)
false
>> not(false)
true
>> not(&&(true, false))
true
```

### `same(arg1, arg2)`

The `same()` function compares two arguments and checks if their string representation is equal to each other. This is similar to the `==()` function but this also works on lists and structs, for which the `==()` does not.

```
>> same(1, 2)
false
>> same(1, 1)
true
>> same(1, `1`)
false
>> same(list(1, 2), list(1, 2))
true
>> ==(list(1, 2), list(1, 2))
false
```

### `random(arg1, arg2)`

The `random()` function takes two numbers as arguments, and returns a random number between the arguments.

```
>> random(1, 90)
67
```

## Variables

In RoyalScript, variables are assigned to literals using the `=()` function. You can only assign one variable in each call to the function. You can use any variable name that starts with a letter, `$`, or `_`, and has the same characters or digits after that. For example:

```
>> =(r, 3)
undefined
>> do(r)
3
>> =(r,e,3)
Argument Error: Got improper arguments but expected 2.
>> =(r,2)
undefined
>> do(r)
2
>> =($$$$, 1000000)
undefined
>> =(%%%, `this is a bad variable name`)
SyntaxError: Unexpected token %
```

You can however reassign an existing variable. If you want to declare a variable with no actual value initially, you can assign it to `null` or `undefined`.

## Grouping Functions

RoyalScript has 2 special functions that are considered "grouping functions". They do not alter the behavior of their arguments, or compute them in anyway. Their purpose it to allow for arguments to other functions to be grouped to override a single argument requirement, or for executing a series of statements in the call of a single function.

### `do(...)`

The `do()` function essentially groups function calls or values togther to be executed or evaluated. It's practicularly useful for displaying a value as a result since all RoyalScript programs require at least one function:

```
>> do(6)
6
>> do(true)
true
>> do(+(2, 3))
5
>> do(=(r, 3), =(r, +(r, r)))
undefined
>> do(=(r, 3), =(r, +(r, r)), r)
6
```

It can also be used to put multiple statements in a loop, though this will be discussed in the loops section.

### `args(...)`

The `args()` function allows the grouping of values, to be called by a function that normally only taks a specific number of arguments. The `args()` function cannot be used to call a series of functions, only to group values.

## Printing

When RoyalScript is run in the browser, one prints arguments by logging them to the console. When RoyalScript is run locally via the NodeJS package, it prints values directly to ther terminal.

### `$(...)`

The `$()` function prints an arbitrary number of values. Here are some examples. Note you wont see this on the playground or REPL as it will go to the console.

```
>> $(1)
1
undefined
>> $(1, 2, 3)
1 2 3
undefined
```

## String Functions

Strings in RoyalScript are types that represent a series of characters or bytes for textual data. They are immutable, but have a series of functions they can be used with for meaningful computation.

**Warning**: Strings in RoyalScript cannot contain newlines, otherwise an error will be raised.

### `&(arg1, arg2)`

The `&()` function takes two string or list operands (also works on lists) and concats them, returning or evaluating to a new string or list.

```
>> &(`h`, `e`)
"he"
>> &(`1`, `2`, `3`)
Argument Error: Got improper arguments but expected 2.
>> &(`12`)
Argument Error: Got improper arguments but expected 2.
```

### `str(arg1)`

The `str()` function converts a number or an expression that evaluates to a number to a string.

```
>> str(4)
"4"
>> str(+(1, 2, 3, 4))
"10"
>> str(+(1, 2, 3, 4), 3)
Argument Error: Got improper arguments but expected 1.
```

### `num(arg1)`

The `num()` function takes one string argument and converts it to a number.

```
>> num(`4`)
4
>> num(str(4))
4
```

### `get(string, index)`

The `get` function, which works for structs and lists as well, takes a string argument and index argument (must be a number for strings) and returns the one character string present at the index. Like other languages, strings are 0-indexed. If the index is out of range, the function evaluates to `undefined`. This behavior can be handled via conditionals which is in a later section.

```
>> =(s, `foo`)
undefined
>> get(s, 1)
"o"
>> get(s, 0)
"f"
>> get(s, 7)
undefined
```

### `len(string)`

The `len()` function gets the length of the string in terms of the number of characters in it.

```
>> len(`foo`)
3
```

### `cut(string, start, end)`

The `cut()` function takes a string, a starting index, and ending index and returns a substring or slice of a string between those indexes. The ending parameter can also be a negative number to specify a reverse index from the end of the string.

```
>> cut(`abcdefgh`, 0, 4)
"abcd"
>> cut(`abcdefgh`, 0, 2)
"ab"
>> cut(`abcdefgh`, 0, -1)
"abcdefg"
>> cut(`abcdefgh`, 0, -4)
"abcd"
```

### `find(string, substr)`

The `find()` function takes a string and substr argument (a string smaller than the first argument), and checks if that substr is in the first argument. If it is, it returns the starting index for it. If it's not in the string, it returns -1.

```
>> find(`appleswafflesavocadoes`, `waffles`)
6
>> find(`appleswafflesavocadoes`, `apples`)
0
>> find(`appleswafflesavocadoes`, `fruits`)
-1
```

### `~(string, regex)`

The `~()` function takes a string argument and a string that represents a regex pattern and perfroms a regex match. It returns true or false if the string is a match or not.

```
>> ~(`hello world!`, `.*`)
true
>> ~(`hello world!`, `.`)
true
>> ~(`hello world!`, `^.$`)
false
>> ~(`hello world!`, `^[a-z !]+$`)
true
>> ~(`hello world!`, `^[a-z]+$`)
false

```

## List Functions

RoyalScript, unlike other functional languages employes the use of mutablee data. One feature of mutable data are lists. Lists are ordered, indexable, collections of multiple types of values.

However, the language allows you to use lists in an immutable fashion as well, which will be explained below.

### `list(...)`

the `list()` function takes an arbitrary number of arguments and returns a list of those arguments. If no arguments are specified, it returns an empty list. Lists can also be nested

```
>> list(1, 2, 3)
[1,2,3]
>> list(1, 2, +(3, 4))
[1,2,7]
>> list(1, true, +(3, 4))
[1,true,7]
>> list(list(3, list()))
[[3,[]]]
```

### `range(start, end)`

The `range()` function creates a list of numbers that start at some value and end at some high value. The start number must be lower than the end number.

```
>> range(0, 5)
[0,1,2,3,4]
>> range(0, 10)
[0,1,2,3,4,5,6,7,8,9]
>> range(0, 0)
[]
```
Ranges can also be used to check if a number is in the indexes of some list.

```
>> in(1, range(0, 5))
true
```
Note though, the `in()` function checkes the keys of a list, not it's values. To find if a value is in a list, use the `find()` function.

### `make(arg1, amount)`

The `make()` function takes one argument and a number, to return a list containing the first argument repeated some specified number of times. This function must have 2 arguments otherwise an error is raised.

```
>> make(true, 5)
[true,true,true,true,true]
>> make(null, 5)
[null,null,null,null,null]
>> make(0, 50)
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
>> make(1)
Argument Error: Got improper arguments but expected 2.
```

### `rep(list)`

The `rep()` function takes a list as an argument and returns a copy of that list. This means that changes to this copy wont effect the previous list. Here is a little example for this behavior.

```
>> =(a, list(1, 2,3))
undefined
>> =(b, rep(a))
undefined
>> do(a)
[1,2,3]
>> do(b)
[1,2,3]
>> put(b, 1)
4
>> do(b)
[1,1,2,3]
>> do(a)
[1,2,3]
```

The rep function is useful for using lists in an immutable fashion. There are also other list functions that return a new copy of the list.

### `len(list)`

Returns the amount of items in the list.

### `cut(list, start, end)`

Identical functionality to `cut()` being used on strings, returns a smaller list of the items between the specified start and end index.

### `get(list, index)`

The `get()` function, much like for strings, takes a list and index arguments, and returns the item in the list present at that index.

```
>> =(a, list(1, 2,3))
undefined
>> get(a, 1)
2
>> get(a, 0)
1
>> get(a, 6)
undefined
```

### `set (list, index, value)`

The `set()` function takes a list, a number index, and a value to set the index of the list equal to the value. Setting the value of an index beyond the length of the list still works, and the values in between will be set to `null`.

```
>> =(e, range(0, 5))
undefined
>> set(e, 2, 60)
60
>> do(e)
[0,1,60,3,4]
>> set(e, 10, 60)
60
>> do(e)
[0,1,60,3,4,null,null,null,null,null,60]
```

**Warning**: To prevent unintended behavior, `set()`, and a few other functions in RoyalScript are considered non-nestable. This means since they aren't meant to evaluate to something, you cannot use them as arguments to other functions, such as:

```
>> str(set(e, 10, 60))
SyntaxError: missing ) after argument list
```

However, this only applies to functions that expect a value. For functions that execute, like conditionals or loops, you can call un-nestable functions like `set()` in their execution spots.

### `append(list, arg1)`

The `append()` function takes a list and another argument and places that argument at the end of the list. It, like set is a non-nestable function

```
>> =(r, list())
undefined
>> append(r, 4)
1
>> do(r)
[4]
```

Even though it only takes one argument to append, it can be extended with the special grouping function called `args()`:

```
>> append(r, args(1, 2, 3))
4
>> do(r)
[4,1,2,3]
```

### `put(list, arg1)`

The `put()` functin appends one argument, to the left or beginning of a list. It can be extended with `args()`. It is non-nestable.

```
>> =(a, range(0, 7))
undefined
>> put(a, 4), do(a)
[4,0,1,2,3,4,5,6]
```

### `insert(list, index, value)`

The `insert()` function takes a list, a number index, and a value, to insert that value at that index, while moving all other values one over to accomodate for the new value. It is non-nestable

```
>> =(a, list(1, 2, 3))
undefined
>> insert(a, 1, true)
[]
>> do(a)
[1,true,2,3]
```

### `remove(list, index)`

Takes a list and a number index, to remove the value at that index and also return it in a new list of it's own. Is nestable.

```
>> =(a, list(1, 2, 34))
undefined
>> remove(a, 1)
[2]
>> do(a)
[1,34]
```

### `find(list, arg1)`

Takes a list and one othr argument, and checks if the argument is a value in the list. If it is, it will return the first index of it recurring. Otherwise, it will return -1. Cannot be extended with `args()`.

```
>> =(a, list(1, 2, true, false, true))
undefined
>> find(a, true)
2
>> find(a, 3)
-1
```

### `&(list1, list2)`

The `&()` function takes two lists as arguments and fuses them together and returns a new list. Cannot be extended with `args()`.

```
>> =(first, list(1))
undefined
>> &(first, list(4))
[1,4]
>> do(first)
[1]
```

## Conditional Functions

RoyalScript employs several unique conditonal functions to facilitate control flow in programs.

### `?(arg1, arg2)`

The `?()` function takes one argument that is or evaluates to a boolean, and another function call of any kind. If that first argument is true, it will execute the second argument, if it's false, nothing happens. Here are some examples:

```
>> ?(true, +(3, 3))
6
>> ?(false, +(1, 3))
undefined
>> =(e, 1)
undefined
>> ?(>(e, 0), =(e, +(e, 2)))
undefined
>> do(e)
3
```

These can also nested and called in a nest.

```
>> ?(==(3, 3),
..     ?(>=(3, 1),
..           list(1, 2, 3)
..            )
..       )
[1,2,3]
```

### `if(bool_exp, true_call, false_call)`

The `if()` function takes a boolean expression, a call/statement to execute if the expression is true, and a third parameter to execute if the expression is false. You must provide 3 arguments or else an error will be raised.

```
>> if(false, 3, 2)
2
>> if(==(1, 1), 3, 2)
3
>> if(==(1, 1),
..      list(1, 1, 1),
..      ?(1, $(1, 2, 3))
..  )
[1,1,1]
```

### `ifs(bool_exp, true_call...)`

The `ifs()` function takes a series of arguments that go by the pattern of one bool expression and one execution to process. The list of arguments should thus be even. This function will execute every statement that has a boolean expression that evaluates to true. Every boolean expression that does not evaluate to true will not be executed.

The `ifs()` is meant to act as an advanced switch statement that works on boolean expressions as opposed to pure pattern matching.

```
>> =(d, 33)
undefined
>> ifs(
..   ==(d, 32), 5,
..   ==(d, 33), $(4),
..   >(d, 10), =(d, +(d, 1))
.. )
4
undefined
>> do(d)
34
```

### `ife(bool_exp, true_call..., else_call)`

The `ife()` function works similarly to the `ifs()` function except it executes one and only one statement. Meaning, it will go through each boolean expression, checking if it is true, and if it is, will execute that statement and stop looking through the other pairs. If none of the pairs are true, it executes the else call.

```
>> =(lst, list())
>> ife(
..   >(len(lst), 2), put(lst, true),
..   ==(len(lst), 0), append(lst, list(2)),
..   !=(len(lst), 0), append(lst, 3),
..   $(`all false`)
..  )
>> do(lst)
[[2]]
```

### `switch([value], case, call_exp...)`

The `switch()` function in RoyalScript facilitates pattern matching, its similar to a series of `==()` functions, but is faster, especcially for numbers.

The switch statements do not fall through between each case pair, meaning the first case that matches will executes, terminating the progression to further cases. There is also no default block for RoyalScript switch statements.

```
>> =(e, `foobar`)
undefined
>> switch(e,
..   0, `good`,
..   `bar`, 5,
..    `foobar`, list(1, 2, 3),
..    1, `r`
..  )
[1,2,3]

```

## Procs (1 or 2 argument functions)

RoyalScript has both general, full length functions that can be defined, but it also has smaller, anonymously created functions called Procs. They are similar to lambdas in other languages.

### `@(parameter, call_exp)`

The `@()` function creates and evaluates to an unnamed, one argument function with only a single execution statement. These are very useful for loops and quick, small areas you need to update values and compute values.

```
>> =(e, @(first, +(first, 3)))
undefined
>> e(3)
6
>> e(8)
11
>> =(e, @(first, +(first, 3), -(first, 3)))
Argument Error: Got improper arguments but expected 2.
```

Procs made with `@()` always return their statement, so you do not need to specify a `return()` function.

Because there first statement is returned, you cannot extend them with `do()`.

### `@@(param1, param2, call_exp)`

The `@@()` works identically to the `@()` except that it evaluates to a proc the has two parameters and returns its only execution statement.

```
>> =(add, @@(a, b, +(a, b)))
undefined
>> add(1, 2)
3
>> add(1, add(1, 3))
5
```

Double parameter procs can also be nested.

### `!@(parameter, call_exp)`

The `!@()` function produces a proc very similarly to the `@()` function except it does not return or evaluate to any value when the proc is called. It's specifically meant to modify mutable arguments like lists or structs, or be used in a loop.

```
>> =(attach, !@(a, append(a, 3)))
undefined
>> =(test, make(4, 10))
undefined
>> attach(test)
undefined
>> do(test)
[4,4,4,4,4,4,4,4,4,4,3]
>> attach(list(1, 2 ,3))
undefined ;does not return the list;
```

Unlike the other procs, however, `!@()` can be extended with `do()`, as in the example below

```
>> =(fnc, !@(a,
..         do(
..            append(a, 1),
..            remove(a, 0),
..            put(a, 3)
..            )
..          )
..      )
undefined
>> =(lst, list(1, 2))
undefined
>> fnc(lst), do(lst)
[3,2,1]
```

## Loop Functions

Unlike most other functional programming languages, RoyalScript has loops, a conditional loop and a for loop. These exist to add more flexibility in writing RoyalScript than purely using recursion as the primary tool of computation.

### `loop(bool_exp, call_exp)`

The `loop()` function in RoyalScript repeatedly calls a single statement while the boolean expression, also called a condition evaluates to true. The call expression, like that in `!@()` can be extended with the `do()` function.

Here is a small, RoyalScript program that uses a loop to sum the values in a list. This is a compiled program, as opposed to the REPL used in the previous examples.

```
=(l, list(1, 2, 3)),
=(i, 0),
=(total, 0),

loop(<(i, len(l)),
      do(
         =(total, +(total, get(l, i))),
         =(i, +(i, 1))
        )
     ),
do(total)
;The result is 6;
```

### `for(list, function_proc)`

The `for()` function in RoyalScript allows one to loop over lists, by calling some function or proc once on each item in the list.

RoyalScript uses a for every type of loop as opposed to traditional C-style type for loops because a functional programming language needs to be able to use functions more than just blocks of code in different arrays of a program.

This is a program that loops over a range and inserts the numbers greater than 3 in reverse order in a new list.

```
=(r, list()),

for(range(0, 10),
         !@( elem,
             do(
                ?(>(elem, 3), insert(r, 0, elem))
                )
             )
         ),
do(r)
;[9,8,7,6,5,4];
```

You can also use functions you define yourself in for loops, which will be explained in the function section.

Something important to understand is that unless directly accessing the name of a list, the for loop cannot modify the list it is iterating over. See this example:

```
=(lst, list(1, 2, 3)),
for(lst,
    !@(elem, +(elem, 2))
    )
,do(lst)
;[1,2,3];
```

## List Construction and Comprehension

In previous examples, lists were constructed in a mutable fashion with loops or functions that had to be specified to filter and/or modify a list. In python, a popular object oriented programming language, it is possible to construct lists through the use of list comprehensions. RoyalScript implements a very similar concept through the `map()` and `filter()` functions, with even more powerful options if you chain them together.

### `map(list, function_proc)`

The `map()` function takes a list and a function or proc(cannot use !@, must return value), and returns a new list that has every element in the old list applied to it. The old list that is called with map is not changed.

Here is an example using a single parameter proc that gets the squares from a range:

```
map(range(0, 10),
         @(x, **(x, 2))
         )
;[0,1,4,9,16,25,36,49,64,81];
```
A double parameter proc, `@@()` can also be used for this:

```
map(range(0, 10),
         @@(a, b, list(a, b))
         )
;[[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
```

### `filter(list, function_proc -> Boolean)`

The `filter()` function in RoyalScript takes a list and a function or proc that takes one argument and returns a boolean value. It goes through a list, and checks if eahc value returns true or false from the proc or function. It then returns a new list for all the values that made the function or proc return true.

This is a program that filters a list for only the even numbers.

```
filter(range(0, 20),
            @(item, ==(0, %(item, 2)))
            )
;[0,2,4,6,8,10,12,14,16,18];
```

To make these more powerful, you can also chain map and filter calls to make a list comprehension function.

```
map(
    filter(
           range(0, 20),
           @(item, ==(0, %(item, 2)))),
    @(elem, //(elem, 3))
    )

;[0,0,1,2,2,3,4,4,5,6];
```
This is a chain using the previous filter and mapping that list to a new list with all the elements in the previous list floor divided by 3.

## Defining Functions

RoyalScript allows you to define your own full size functions, which take any amount of parameters and have any amount of statements you want. RoyalScript also uses a special function, `return()`, which allows you to return nothing, one value, or multiple values in a list.

### `def(name, args_exp, call_exp...)`

The `def()` function is the function which defines new named functions. Functions are much more flexible and powerful than procs, because they are named, they can have any amount of parameters, any amount of call expressions, and either return a value, or not, or have multiple return statements in different parts of the function, such as with control flow.

This is a simple function with calculates the factorial of some number recursively.

```
def(factorial,
    args(n),
    if(==(n, 1),
         return(1),
         return(
                *(n, factorial(-(n, 1)))
                )
         )
    )
,do(factorial(5))
;120;
```

You can also define functions with no parameters:

```
def(noparams,
    args(),
    return(true, true)
    )
, do(noparams())
;[true,true];
```

However, you can't define a function that is already a standard library function in RoyalScript.

```
def(range,
    args(),
    return(list())
    )
, do(noparams())
;Illegal Name Error: Cannot choose reserved function name;
```

You can also use a `_` if you are using a function for a loop or map or filter that you don't need the name of.

Here is an example using a larger function for mapping

```
map(range(0, 10),
         def(_, args(a),
                ;prints the value to console;
                $(a),
                return(a, +(a, 1))
                )
         )
;1
2
3
4
5
6
7
8
9;
;[[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10]];
```

## Structs

Perhaps the most unique feature to RoyalScript are structs. Structs are groups of data fields that don't have a specified type in each field, but are named. They are mutable, and can be passed around as references between functions. You create structs with the `new()` function. Every struct also has it's own type callable with the `type()` function.

### `struct(name, field...)`

The `struct()` function defines a new struct that can be initialized. Each struct can have any amount of fields. You don't have to initialize a struct with all it's field called either. However, like the defined functions you cannot use a name that is already taken by a native RoyalScript function.

```
struct(food,
       calories, protein, fat, carbs
       ),
new(food, 100, `5g`, `1g`, `10g`)

;{"calories":100,"protein":"5g","fat":"1g","carbs":"10g"};
```

Or with no fields argued.

```
struct(food,
       calories, protein, fat, carbs
       ),
new(food),
;{};
```

You can also put procs or functions on a struct:

```
struct(bank,
       deposit, balance
       ),
=(a, new(bank, @(a, a), 50)),
do(
   get(a, `balance`)
   )
;50;
```

### `new(name, arg...)`

The `new()` function creates a new instance of a struct. You must use a previously defined struct, as RoyalScript does not support generic objects or keyed-value collections. Lists can accept a string as a key, but this behavior is not recommended or type/method safe.

You can also have functions return new instances of structs

```
def(factory,
    args(amount, size),
    struct(machine, size, built),
    return(
           make(new(machine, size, true), amount)
           )
    ),
factory(5)
;[{"built":true},{"built":true},{"built":true},{"built":true},{"built":true}];
```

### `type(struct_instance)`

The `type()` function checks the type of some struct instantance, returning it as a string value.

```
struct(bank,
       deposit, balance
       ),
=(a, new(bank, @(a, a), 50)),
type(a)
;"bank";
```

Another technique is also to use a switch statement with the type function called on some argument, so you can handle different types of structs much quicker and faster.

**Warning**: It's recommended not to use the `type()` function on primtive types in RoyalScript, such as lists or numbers, because primitive types do not have constructors like structs do.


text:
from module_kits.vtk_kit.mixins import SimpleVTKClassModuleBase
import vtk

class vtkExtractSelection(SimpleVTKClassModuleBase):
    def __init__(self, module_manager):
        SimpleVTKClassModuleBase.__init__(
            self, module_manager,
            vtk.vtkExtractSelection(), 'Processing.',
            ('vtkDataSet', 'vtkSelection'), ('vtkDataSet',),
            replaceDoc=True,
            inputFunctions=None, outputFunctions=None)


text:
package stream.rate

import akka.actor.{ActorSystem, Cancellable}
import akka.stream.stage._

import scala.concurrent.duration.FiniteDuration

/**
 * Emits elements with intervals specified by a Duration Source.
 *
 * The duration between elements is determined by the supplied Duration iterator.
 * The purpose is to enable variable timing between elements to simulate real-world events that
 * may follow a statistical distribution (Poisson, for example).
 *
 * The scheduling algorithm is intentionally simple and does not attempt to perform adjustments to
 * match actual with desired element timing/model.rate.
 * Scheduling is done using the Akka scheduler, so any caveats about timing regarding that scheduler, notably
 * its granularity, are applicable here. The actual emission model.rate is likely to be different (less) than desired.
 *
 * @param delays desired inter-event timings
 */
case class DelayStage[T](delays: Iterator[FiniteDuration])(implicit system: ActorSystem)
    extends AsyncStage[T, T, T]
{
  var delay: FiniteDuration = _
  var cancellable = Option.empty[Cancellable]

  override def onAsyncInput(event: T, ctx: AsyncContext[T, T]): Directive = {
    cancellable = None
    if (ctx.isFinishing) ctx.pushAndFinish(event)
    else ctx.pushAndPull(event)
  }

  override def onPush(elem: T, ctx: AsyncContext[T, T]): UpstreamDirective = {
    if (delays.hasNext) {
      val delay = delays.next()
      val callback = ctx.getAsyncCallback()

      // Schedule element to be emitted after delay
      import system.dispatcher
      cancellable = Some(system.scheduler.scheduleOnce(delay){ callback.invoke(elem) })

      ctx.holdUpstream()
    } else {
      ctx.finish()
    }
  }

  override def onPull(ctx: AsyncContext[T, T]): DownstreamDirective = ctx.holdDownstream()

  override def onUpstreamFinish(ctx: AsyncContext[T, T]): TerminationDirective =
    cancellable.map(_ => ctx.absorbTermination).getOrElse(super.onUpstreamFinish(ctx))

  override def onUpstreamFailure(cause: Throwable, ctx: AsyncContext[T, T]): TerminationDirective = {
    cancellable.map(_.cancel)
    cancellable = None
    super.onUpstreamFailure(cause, ctx)
  }
}